ios 如何在 UIButton 子类中设置 UIButton 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10277863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set UIButton type in UIButton Subclass
提问by Haris Hussain
I am subclassing the UIButton, what i want is to set the button type to Round Rect.
我正在继承 UIButton,我想要的是将按钮类型设置为 Round Rect。
Button.h
按钮.h
@interface Button : UIButton {}
- (void)initialize;
@end
Button.m
按钮.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
Here i tried [UIButton buttonWithType:UIButtonTypeRoundedRect]
but it doesn't work. Can anyone suggest how to make it work?
在这里我试过了,[UIButton buttonWithType:UIButtonTypeRoundedRect]
但它不起作用。谁能建议如何使它工作?
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.
我知道在以前的许多帖子中都说过不推荐对 UIButton 进行子类化,但事实上在开发人员的文档中没有提到不要对它进行子类化。
采纳答案by Caleb
You may find the discussion at CocoaBuilder's thread How to subclass UIButton?helpful, particularly Hyman Nutting's suggestion to ignore the buttonType:
您可以在 CocoaBuilder 的线程如何对 UIButton 进行子类化?有用,尤其是Hyman·纳丁的建议忽略按钮类型:
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)
请注意,这样 buttonType 不会显式设置为任何内容,这可能意味着它是 UIButtonTypeCustom。Docs 似乎并没有真正指定这一点,但由于这是枚举中的 0 值,这很可能会发生(这似乎也是可观察到的行为)
回答by Confused Vorlon
not quite what you're looking for, but remember that your subclass still has the buttonWithType method, and it works fine.
不完全是您要找的,但请记住,您的子类仍然具有 buttonWithType 方法,并且它工作正常。
buttonWithType calls your subclasses initWithFrame, and sets the type appropriately.
buttonWithType 调用您的子类 initWithFrame,并适当地设置类型。
SubclassButton *myButton=[SubclassButton buttonWithType:UIButtonTypeRoundedRect];
回答by Mobile Dan
UIButton
subclass in Swift
UIButton
Swift 中的子类
*The following code works in Swift 3 and above.
*以下代码适用于 Swift 3 及更高版本。
You cannot set the buttonType
of a UIButton
subclass by design. It is automatically set to custom
which means you get a plain appearance and behavior as a starting point.
你不能设置buttonType
一个的UIButton
设计子类。它会自动设置为custom
这意味着您将获得简单的外观和行为作为起点。
If you want to reuse code that sets the button's appearance, you can do this without subclassing. One approach is by providing factory methods that create a UIButton
and set visual properties.
如果您想重用设置按钮外观的代码,您可以在不进行子类化的情况下执行此操作。一种方法是提供创建UIButton
和设置可视化属性的工厂方法。
Example factory method to avoid subclassing
避免子类化的工厂方法示例
extension UIButton {
static func createStandardButton() -> UIButton {
let button = UIButton(type: UIButtonType.system)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.setTitleColor(UIColor.black, for: .normal)
button.setTitleColor(UIColor.gray, for: .highlighted)
return button
}
}
let button = UIButton.createStandardButton()
I avoid subclassing UIButton
when other customization techniques suffice. The factory method example is one option. There are other options including the UIAppearance API, etc.
UIButton
当其他定制技术足够时,我避免子类化。工厂方法示例是一种选择。还有其他选项,包括 UIAppearance API 等。
Sometimes there are customization needs that require subclassing. For example, I've created UIButton
subclasses to take fine control over how the button animates in response to touch events or to have the button call a predefined delegate or closure when tapped (as opposed to assigning a #selector
to each instance).
有时存在需要子类化的定制需求。例如,我创建了UIButton
子类来精细控制按钮如何响应触摸事件而设置动画,或者让按钮在点击时调用预定义的委托或闭包(而不是#selector
为每个实例分配 a )。
The following is a basic UIButton
subclass example as a starting point.
以下是UIButton
作为起点的基本子类示例。
Example UIButton Subclass
示例 UIButton 子类
internal class CustomButton: UIButton {
init() {
// The frame can be set outside of the initializer. Default to zero.
super.init(frame: CGRect.zero)
initialize()
}
required init?(coder aDecoder: NSCoder) {
// Called when instantiating from storyboard or nib
super.init(coder: aDecoder)
initialize()
}
func initialize() {
print("Execute common initialization code")
}
}
let button = CustomButton()
print(button.buttonType == .custom) // true
A note about UIButtonType
一个关于 UIButtonType
Since the question was asked in 2012, UIButtonType.roundedRect
has been deprecated. The header file comments say to use UIButtonType.system
instead.
自 2012 年提出该问题以来,UIButtonType.roundedRect
已弃用。头文件注释说改为使用UIButtonType.system
。
The following is from UIButton.h converted to Swift in Xcode
以下是从 UIButton.h 在 Xcode 中转换为 Swift
public enum UIButtonType : Int {
case custom // no button type
@available(iOS 7.0, *)
case system // standard system button
case detailDisclosure
case infoLight
case infoDark
case contactAdd
public static var roundedRect: UIButtonType { get } // Deprecated, use UIButtonTypeSystem instead
}
回答by Chris
With iOS7, this is more relevant than before, as sometimes you needto use UIButtonTypeSystem, and you can't simply override init and do [MyButton alloc] init], because then it's a UIButtonTypeCustom, which doesn't reflect tintColor nor have nice highlight fade effects.
在 iOS7 中,这比以前更相关,因为有时您需要使用 UIButtonTypeSystem,并且您不能简单地覆盖 init 并执行 [MyButton alloc] init],因为那时它是一个 UIButtonTypeCustom,它不反映 tintColor 也不具有 nice突出淡入淡出效果。
To subclass, make a static constructor like so:
要子类化,请创建一个静态构造函数,如下所示:
+ (instancetype)myButtonWithTitle:(NSString *)title imageNamed:(NSString *)imageName target:(id)target action:(SEL)action {
MyButton *button = [self buttonWithType:UIButtonTypeSystem];
... my custom setup for title, image, target, etc ...
return button;
}
回答by Duncan Groenewald
You can definitely subclass UIButton. I have successfully created a DateButton which looks like a UITextField but when the user touches it it displays a DatePicker in a Popover. Has a property for storing the Date, etc. Let me know if the above never solved your problem and I'll post details more.
你绝对可以继承 UIButton。我已经成功创建了一个 DateButton,它看起来像一个 UITextField,但是当用户触摸它时,它会在 Popover 中显示一个 DatePicker。有一个用于存储日期等的属性。如果上述内容从未解决您的问题,请告诉我,我会发布更多详细信息。
回答by Chakalaka
What about this?
那这个呢?
- (id)initWithFrame:(CGRect)frame
{
self = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (self) {
self.frame = frame;
}
return self;
}