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

问题描述 投票:0回答:2

我在.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时,它应该像我以编程方式那样工作。

ios swift xcode uiview interface-builder
2个回答
1
投票

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

Assigning UIView As you CustomView


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

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

© www.soinside.com 2019 - 2024. All rights reserved.