[英]Programmatically created UIViewController loading an Interface builder view.xib file
[英]Custom view created with Xib not working in Interface Builder
我已经在.Xib
文件的帮助下创建了一个自定义视图。 当我以编程方式创建视图并将其添加到我的ViewController时,它可以正常工作。 但是,如果我在Interface Builder中创建一个UIView并将该类设置为我的CustomView
类并运行它,它将不会显示。
这是我的CustomView
类中的代码:
@IBOutlet var view: UIView!
init() {
super.init(frame:CGRect.zero)
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setup()
}
func setup() {
Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
view.backgroundColor = UIColor(red: 10.0/255.0, green: 30.0/255.0, blue: 52.0/255.0, alpha: 1.0)
view.translatesAutoresizingMaskIntoConstraints = false
view.isUserInteractionEnabled = true
}
func presentInView(superView:UIView) {
superView.addSubview(view)
// Define Constraints
let height = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 90.0)
view.addConstraint(height)
let topConstraint = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: superView, attribute: .top, multiplier: 1.0, constant: 0.0)
let leftConstraint = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: superView, attribute: .left, multiplier: 1.0, constant: 0.0)
let rightConstraint = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: superView, attribute: .right, multiplier: 1.0, constant: 0.0)
superView.addConstraints([topConstraint,leftConstraint, rightConstraint])
}
在.Xib
文件,我设置了类的Files Owner
给CustomView
和连接IBOutlet中view
在主视图.Xib
。
在我的ViewController中,我这样做是为了向其中添加CustomView
:
let customView = CustomView()
customView.presentInView(superView: self.view)
当我在Interface Builder中添加UIView
,它应该像以编程方式进行时一样工作。
问题是view
的大小,即我连接到.Xib
文件中的view
的IBOutlet
。 当我用代码创建CustomView
时,我还调用了presentInView
方法,我认为在Interface Builder中创建该方法是不必要的,因为我在那里设置了约束。 但是我忘记了,我在Interface Builder中设置的约束是针对类本身而不是其view
出口的。 因此, view
需要约束以将其保留在CustomView
视图中,这不是ViewController.view
而是CustomView
。 那就是为什么我也必须写self.addSubview(view)
通过这些更改,它可以工作。 这是最终代码:
func setup() {
Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
view.backgroundColor = UIColor(red: 10.0/255.0, green: 30.0/255.0, blue: 52.0/255.0, alpha: 1.0)
view.translatesAutoresizingMaskIntoConstraints = false
view.isUserInteractionEnabled = true
self.addSubview(view)
let topConstraint = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0)
let leftConstraint = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0)
let rightConstraint = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0)
let bottomConstraint = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0)
self.addConstraints([topConstraint,leftConstraint, rightConstraint, bottomConstraint])
}
不再需要presentInView
方法。
希望如果有人遇到这样的问题会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.