【发布时间】:2017-06-28 12:04:57
【问题描述】:
我正在继承一个 UIView,我想在其中添加一个约束。但是当我这样做时,我无法添加任何约束:
class A19: UIView {
let titleLabel : UILabel = {
let label = UILabel()
label.text = "test"
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
init() {
super.init(frame: CGRect(x: 0, y: 0, width: 414, height: 250))
backgroundColor = .black
addSubview(titleLabel)
titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
titleLabel.sizeToFit()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这有什么问题?
【问题讨论】:
-
这看起来不错...你如何将它添加为子视图?
-
不,只是在操场上玩……看看我的编辑。
-
这一行:
titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true等于什么顶级锚?self?view?还是别的什么?