我使用自定义框架加载了 nib View ,但仅在 superView 顶部显示该 nib View 。
我在 viewDidLoad 中调用这段代码。
let mb = Bundle.main.loadNibNamed("MBViewNib", owner: self, options: nil)?.first as! MBView
mb.frame = CGRect(x: 50, y: 200, width: 100, height: 100)
view.addSubview(mb)
最佳答案
我们可以使用这样的东西:
mb = Bundle.main.loadNibNamed("MBViewNib", owner: self, options: nil)?.first as? MBView
view.addSubview(mb!)
mb.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint(item: mb, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 414))
view.addConstraint(NSLayoutConstraint(item: mb, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: 64))
view.addConstraint(NSLayoutConstraint(item: mb, attribute: .left, relatedBy: .equal, toItem: self.bottomLayoutGuide, attribute: .left, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: mb, attribute: .right, relatedBy: .equal, toItem: self.bottomLayoutGuide, attribute: .right, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: mb, attribute: .bottom, relatedBy: .equal, toItem: self.bottomLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0))
它在 Swift 4 中对我有用。
关于ios - 如何更改 iOS Swift 4 中加载的 NibView 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533586/