UIButtonType 这是个只读属性,意味着你不能通过以下语句来设置它:

当你创建一个 UIButton 对象的时候,如果没有指定 UIButtonType,那么默认将被设置成 .custom。那如何在子类中设置呢?可以用如下方法:
import UIKit
class customButton: UIButton {
static func createButton() -> customButton {
let button = customButton(type: .system)
button.setTitle("我是按钮", for: .normal)
return button
}
}
然后这么调用:
let button = customButton.createBackButton()
另外,不通过创建子类也能实现(工厂方法):
extension UIButton {
static func createButton() -> UIButton {
let button = UIButton(type: .system)
button.setTitle("我是按钮", for: .normal)
return button
}
}
调用:
self.button = UIButton.createButton()
网友评论