【发布时间】:2016-03-22 18:54:10
【问题描述】:
我创建了一个自定义 UIView,里面有一个 UIButton 当我点击它时,它会简单地改变它的背景颜色。
我想知道如何在此自定义 UIView 的所有实例中更改此按钮的背景颜色。
有什么想法吗?
import UIKit
class RadioChannelView: UIView {
var playButton:UIButton!
init(frame: CGRect, themeColor: UIColor) {
super.init(frame: frame)
playButton = UIButton(type: .Custom)
playButton.backgroundColor = UIColor.blackColor()
playButton.layer.cornerRadius = 40
playButton.addTarget(self, action: "playButtonTapped", forControlEvents: .TouchUpInside)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func playButtonTapped() {
self.playButton.backgroundColor = UIColor.redColor()
}
}
【问题讨论】:
-
您必须遍历所有子视图并更改该按钮的背景颜色。
-
你可以做
UIButton.appearanceWhenContainedInInstancesOfClasses([RadioChannelView.self]).backgroundColor = UIColor.redColor()