ios – 如何在UIButton子类中设置UIButton类型

我将UIButton子类化,我想要的是将按钮类型设置为Round Rect.

Button.h

@interface Button : UIButton {}
    - (void)initialize;
@end

Button.m

@implementation Button

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


-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self){
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.textColor = [UIColor redColor];
    self.titleLabel.textAlignment = UITextAlignmentCenter;
   //[UIButton buttonWithType:UIButtonTypeRoundedRect];
}

@end

在这里我尝试[UIButton buttonWithType:UIButtonTypeRoundedRect],但它不工作.任何人都可以建议如何使其发挥作用?

我知道在许多以前的帖子中已经说过,SubBlassing UIButton是不推荐的,但事实上,在开发人员的文档中没有提到没有子类化它.

解决方法

您可能会发现CocoaBuilder的线程 How to subclass UIButton?的讨论有帮助,特别是 Jack Nutting’s suggestion to ignore the buttonType

Note that this way the buttonType isn’t explicitly set to anything,which probably means that it’s UIButtonTypeCustom. The Docs don’t seem to actually specify that,but since that’s the 0 value in the enum,that’s likely what happens (and that seems to be the observable behavior as well)

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...