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

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

问题描述

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

I am subclassing the UIButton, what i want is to set the button type to 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] 但它不起作用。任何人都可以建议如何让它工作吗?

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

我在之前的很多帖子中都知道不推荐使用子类化UIButton,但是在Developer's Docs中有这样的事实没有提及NOT子类化它。

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。 Docs没有
似乎实际指定了,但由于这是
枚举中的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天全站免登陆