繁体   English   中英

用Xib创建的自定义视图在Interface Builder中不起作用

[英]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 OwnerCustomView和连接IBOutlet中view在主视图.Xib

在我的ViewController中,我这样做是为了向其中添加CustomView

let customView = CustomView()
customView.presentInView(superView: self.view)

当我在Interface Builder中添加UIView ,它应该像以编程方式进行时一样工作。

您是否在Interface Builder中将CustomView类分配给uiview。

在您自定义View时分配UIView

问题是view的大小,即我连接到.Xib文件中的viewIBOutlet 当我用代码创建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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM