gpt4 book ai didi

ios - 如何为客户 UIButon 设置属性

转载 作者:行者123 更新时间:2023-11-29 03:14:54 25 4
gpt4 key购买 nike

我有一个按钮,我正在尝试为其添加边框:.h

@interface MyButton : UIButton

.m

@implementation MyButton

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {


}

self.layer.borderWidth=1.0f;
self.layer.cornerRadius = 4;
self.layer.borderColor=[[UIColor blueColor] CGColor];

return self;
}

没有设置边框,我做错了什么?

最佳答案

UIButton 不应该被子类化,如果您只想设置样式按钮,我建议您使用类别。这是一个示例类别来完成您想要的...请注意,这尚未通过编译器运行,但您明白了:

UIButton+Styles.h

#import <UIKit/UIKit.h>

@interface UIButton (Styles)

-(void)styleWithBorderColor:(UIColor *)color;
@end

.m

#import "UIButton+Styles.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIButton (CURStyles)
-(void)styleWithBorderColor:(UIColor *)color
{
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 4.0f;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = color.CGColor;
}
@end

现在,当您想要设置按钮样式时,只需导入类别并像这样调用它:

[myButton styleWithBorderColor:[UIColor whiteColor]];

编辑:请查看评论...这些应该加上前缀以避免与框架方法混淆

关于ios - 如何为客户 UIButon 设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21803903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com