【发布时间】:2016-01-05 18:49:27
【问题描述】:
我正在使用此代码在我的主 ViewController 上自定义 UIButton,并将其放置在它自己的单独的 .swift 文件中,以便在 UIButton 存在的任何地方引用。我希望能够在按下按钮后从我的主 ViewController 中修改 MyCustomButton 的属性。 例如,我按下了一个使用 MyCustomButton 属性的按钮,它的颜色发生了变化。
我刚刚开始接触 swift,还不太熟悉它的来龙去脉。任何帮助表示赞赏。
import Foundation
import UIKit
class MyCustomButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.layer.cornerRadius = 25;
self.layer.borderColor = UIColor.blackColor().CGColor
self.layer.borderWidth = 3
self.backgroundColor = UIColor.redColor()
self.tintColor = UIColor.whiteColor()
self.setTitle("", forState: UIControlState.Normal)
self.titleLabel?.numberOfLines = 0
self.titleLabel?.textAlignment = NSTextAlignment.Center
}
}
【问题讨论】: