【问题标题】:Best way to let multiple UIButtons share the same appearance让多个 UIButton 共享相同外观的最佳方法
【发布时间】:2017-05-06 16:13:32
【问题描述】:

让多个按钮共享相同外观的最佳做法是什么?

现在我正在像这样循环创建这些按钮:

for i in 0..<10{
    let button = UIButton()
    button.setTitle(String(i), for: .normal)
    button.backgroundColor = #colorLiteral(red: 0.3137254902, green: 0.2745098039, blue: 0.2745098039, alpha: 0.5977632705)
    numberButtons.append(button)
    button.layer.borderColor = #colorLiteral(red: 0.04296875, green: 0.04296875, blue: 0.04296875, alpha: 0.5).cgColor
    button.layer.borderWidth = 1.0
    button.addTarget(self, action:  #selector(buttonAction), for: .touchUpInside)
}

Swift 做这些事情的方法是什么?子类 UIButton?或者添加一些初始化方法作为扩展?

【问题讨论】:

标签: ios swift uibutton


【解决方案1】:

1- 创建UIButton 的子类。

2- 将所有自定义项放入其构造函数中。

3- 将它用于您的按钮(在故事板或代码中)。

answer 中的讨论将帮助您继承 UIButton 并以您想要的方式对其进行自定义。

这个answer 提供了关于何时使用扩展以及何时使用继承的一般经验法则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多