![](/img/trans.png)
[英]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.