正如其他人在这里指出的,你不能继承UIRoundedRectButton。但是你可以通过在init方法中设置一些CALayer属性来让你的按钮看起来像圆角矩形按钮。
#import "MyCustomButton.h"
#import <QuartzCore/QuartzCore.h>
@implementation MyCustomButton
- (id)initWithFrame:(CGRect)frame
//button created in code
{
self = [super initWithFrame:frame];
if (self) {
[self initialise];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder;
//button created in Interface Builder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialise];
}
return self;
}
- (void)initialise;
{
self.layer.cornerRadius = 10.0f;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
}
@end
如果你已经自动版式禁用您可以继续使用“圆角的矩形”按钮风格IB给你布局正确的外观,虽然风格将您的自定义按钮加载时被忽略。如果启用了自动布局功能,则必须将IB样式更改为“自定义”,否则您会发现按钮框架的行为不符合您的期望。
根据我的经验,不,IB不能解释您的设计决策(尤其是那些内置的代码)并在运行之前渲染它们。 – 2013-02-08 16:26:55
您不能在Interface Builder中使用您的子类。您必须以编程方式将它们添加到视图中 – Luke 2013-02-08 17:07:45
@Luke:您可以在IB中使用UIButton子类 - 我一直都在使用它。当我自定义按钮(这是99%的时间)时,这工作正常,但如果我想要一个子类型的圆角矩形按钮,显然不起作用。 – MusiGenesis 2013-02-08 17:20:55