使用Xib创建的自定义视图无法在Interface Builder中使用

时间:2016-11-15 14:53:18

标签: ios swift xcode uiview 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时,它应该像我以编程方式那样工作。

2 个答案:

答案 0 :(得分:1)

您是否在界面生成器中为您的uiview分配了CustomView类。

Assigning UIView As you CustomView

答案 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方法。

希望如果其他人遇到这样的问题会有所帮助。