【发布时间】:2016-03-11 22:37:18
【问题描述】:
我有很多相同size 的按钮,我想从惰性变量中设置恒定宽度,我应该怎么做? JRTopView.buttonWidth 和 buttonWidth 都不起作用。
class JRTopView: UIView {
let buttonWidth:CGFloat = 150
lazy var leftButton: UIButton! = {
let btn = UIButton(type: UIButtonType.Custom)
btn.backgroundColor = UIColor.greenColor()
btn.frame = CGRectMake(-30, 30, JRTopView.buttonWidth, buttonWidth)
return btn
}()
lazy var rightButton: UIButton! = {
let btn = UIButton(type: UIButtonType.Custom)
btn.backgroundColor = UIColor.greenColor()
btn.frame = CGRectMake(-30, 30, JRTopView.buttonWidth, buttonWidth)
return btn
}()
}
谢谢!
编辑:
有趣的是,如果我使用self.buttonWidth,它在leftButton 中有效,但在rightButton 中无效。
【问题讨论】:
-
JRTopView.buttonWidth不起作用,因为 buttonWidth 是实例属性而不是计算的类属性。尝试使用self.buttonWidth。