如何在 UIButton 子类中设置 UIButton 类型 [英] how to set UIButton type in UIButton Subclass

查看:23
本文介绍了如何在 UIButton 子类中设置 UIButton 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在继承 UIButton,我想要的是将按钮类型设置为 Round Rect.

I am subclassing the UIButton, what i want is to set the button type to Round Rect.

按钮.h

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

按钮.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] 但它不起作用.谁能建议如何使它工作?

Here i tried [UIButton buttonWithType:UIButtonTypeRoundedRect] but it doesn't work. Can anyone suggest how to make it work?

我知道在之前的许多帖子中都说不推荐对 UIButton 进行子类化,但事实上在 Developer's Docs 中没有提到不对其进行子类化.

I know in many previous post it has been said that Subclassing UIButton is not recommended, but the fact that in Developer's Docs there is no mention about NOT subclassing it.

推荐答案

您可以在 CocoaBuilder 的线程中找到讨论 如何继承 UIButton? 很有帮助,特别是 Jack Nutting 建议忽略 buttonType:

You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:

请注意,这种方式 buttonType 没有明确设置为任何东西,这可能意味着它是 UIButtonTypeCustom.文档没有似乎实际上指定了这一点,但因为这是 0 中的值枚举,这很可能会发生(这似乎是可观察的行为)

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)

这篇关于如何在 UIButton 子类中设置 UIButton 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆