我一直在尝试为我的应用设计可重用的组件。它主要用于学习目的。
我'尝试以编程方式设置约束,但Interface Builder
不断添加自动约束:
<NSIBPrototypingLayoutConstraint:0x7fae02731800 'IB auto generated at build time for view with fixed frame' H:[_1.TOHeader:0x7fae02718c20(89)]>
多数民众赞成打破我的。
这是我的代码:
import UIKit
class TOHeader: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
println("init aDecoder")
self.backgroundColor = UIColor(red: 255, green: 0, blue: 0, alpha: 1)
self.setTranslatesAutoresizingMaskIntoConstraints(false)
}
override func willMoveToSuperview(newSuperview: UIView?) {
super.willMoveToSuperview(newSuperview)
println("willMoveToSuperview()")
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
superview!.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: superview!, attribute: .Left, multiplier: 1.0, constant: 0.0))
superview!.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: superview!, attribute: .Width, multiplier: 1.0, constant: 0.0))
addConstraint(NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 42))
}
}