UIButton 子类 - 设置属性?
我创建了 UIButton 的子类,给它一个白色的 2px 无半径边框,现在我尝试根据按钮状态“全局”设置它的字体、字体颜色和背景颜色。
字体未设置。颜色或背景颜色也没有。这是怎么回事?这是我的代码。我希望你能帮忙:)
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[self titleLabel] setFont:[UIFont fontWithName:@"ZegoeUI" size:18]];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
if(self.state == UIControlStateHighlighted) {
[self setBackgroundColor:[UIColor whiteColor]];
}
else {
[self setBackgroundColor:[UIColor blackColor]];
}
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
[self.layer setMasksToBounds:YES];
[self.layer setBorderWidth:2.0];
[self.layer setCornerRadius:0.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:1.0] CGColor]];
}
我不认为我做错了什么。我有这个类,并在 IB 中链接了按钮,并设置了类类型。
I've created a Subclass of UIButton to give it a white 2px No Radii border, and now I'm trying to "globally" set it's font, font color, and background color depending on the button state.
The font does not set. Nor do the colors or the background colors. What's going on? Here's my code. I hope you can help :)
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[self titleLabel] setFont:[UIFont fontWithName:@"ZegoeUI" size:18]];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
if(self.state == UIControlStateHighlighted) {
[self setBackgroundColor:[UIColor whiteColor]];
}
else {
[self setBackgroundColor:[UIColor blackColor]];
}
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
[self.layer setMasksToBounds:YES];
[self.layer setBorderWidth:2.0];
[self.layer setCornerRadius:0.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:1.0] CGColor]];
}
I don't think I'm doing anything wrong. I have this class, and have linked Buttons in IB, and set the class type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在 IB 中创建了自定义子类按钮,则不会调用
initWithFrame:
。您需要重写initWithCoder:
,或者最好创建一个从initWithFrame:
和initWithCoder:
调用的单独设置方法。对于第一种情况:
If you have created the custom subclass buttons in IB,
initWithFrame:
will not be called. You need to overrideinitWithCoder:
, or, preferably, create a separate setup method that is called both frominitWithFrame:
andinitWithCoder:
.For the first case:
我是这样做的(没有 IB):
也适用于其他类型,
UIButtonTypeSystem
只是一个示例。Here's how I do it (without IB):
Works also with other types,
UIButtonTypeSystem
is just an example.UIButton 是一个类簇。您确实不应该子类化,因为它代表的类是私有的。
UIButton is a class cluster. You really shouldn't be subclassing as the classes it represents are private.