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

how to set UIButton type in UIButton Subclass(如何在 UIButton 子类中设置 UIButton 类型)
本文介绍了如何在 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 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)