我已经在.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
时,它应该像我以编程方式那样工作。
答案 0 :(得分:1)
答案 1 :(得分:1)
问题是我在view
文件中连接到我的视图的IBOutlet
,.Xib
的大小。当我在代码中创建CustomView
时,我也调用了presentInView
方法,我认为在Interface Builder中创建这个方法是不必要的,因为我在那里设置了约束。但是我忘记了,我在Interface Builder中设置的约束是针对类本身而不是针对它的view
插座。因此view
需要约束才能将其保留在超级视图中,而不是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
方法。
希望如果其他人遇到这样的问题会有所帮助。