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

标签 ios uibutton subclass

我正在子类化 UIButton,我想要的是将按钮类型设置为 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] 但它不起作用。谁能建议如何让它发挥作用?

我知道在之前的很多帖子中都说不推荐对 UIButton 进行子类化,但事实上在开发者文档中没有提到不对它进行子类化。

最佳答案

UIButton Swift 中的子类

*以下代码适用于 Swift 3 及更高版本。

您不能通过设计设置 UIButton 子类的 buttonType。它会自动设置为 custom,这意味着您将获得一个普通的外观和行为作为起点。

如果你想重用设置按钮外观的代码,你可以在没有子类化的情况下做到这一点。一种方法是提供创建 UIButton 和设置视觉属性的工厂方法。

避免子类化的示例工厂方法

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()

当其他自定义技术足够时,我避免子类化 UIButton。工厂方法示例是一种选择。还有其他选项,包括 UIAppearance API 等。

有时有需要子类化的定制需求。例如,我创建了 UIButton 子类来很好地控制按钮如何响应触摸事件进行动画处理,或者让按钮在点击时调用预定义的委托(delegate)或闭包(而不是分配 #selector 到每个实例)。

以下是作为起点的基本 UIButton 子类示例。

示例 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

关于UIButtonType的注释

自 2012 年提出该问题以来,UIButtonType.roundedRect 已被弃用。头文件注释说要改用 UIButtonType.system

以下是从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
}

关于ios - 如何在 UIButton 子类中设置 UIButton 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277863/

相关文章:

ios - 获取当前 ViewController 的子类 navigationController

ios - 将 Core Data 语句等同于 SQLite 语句(按 ... desc.. 排序)

ios - 为什么将我的 View Controller 添加到导航 Controller 似乎会干扰我的约束?

ios - 网址 session |身份验证质询响应 | NTLM

ios - Mixpanel iOS - 如何 MPTweakBind UIButton 文本?

ios - UIButton 按下时不会更改文本颜色

java - 具有 2 个来自子类的构造函数的 Arraylist

java - 子类化 ParseUser 时出现 ClassCastException (Parse.com)

ios - ionic 2 : iOS Push notification - Register ID Error

ios - XIB 中用作自定义 UITableViewCell 的自定义按钮不响应点击 (ios7)