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


基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01