【发布时间】:2016-11-06 14:41:30
【问题描述】:
我正在使用以下方法将我的 NIB 文件加载到我的视图中:
override init(frame: CGRect) {
super.init(frame: frame)
Bundle.main.loadNibNamed("EventPopup", owner: self, options: nil)
//self.frame = self.view.bounds - DIDN'T WORK
//self.frame.size.width = 300 - DID'T WORK
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
Bundle.main.loadNibNamed("EventPopup", owner: self, options: nil)
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
这有效并添加了我的 NIB,但 UIView 的宽度延伸到父容器之外。这仅在将自动布局添加到 uiview 后发生。我检查过,初始帧大小为 600x600。
即使在尝试通过调用 layoutSubviews() 来重置 NIB 大小并将新的框架约束放入其中之后,仍然拒绝调整大小。例如:
override func layoutSubviews() {
// self.frame.size.width = 300
//self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: 200, height: self.frame.height)
}
【问题讨论】:
标签: swift autolayout nib