如何在 iOS Swift 4 中更改加载的 NibView 的位置?
How to change position of loaded NibView in iOS Swift 4?
我用自定义框架加载了 nib 视图,但只是在 superView 顶部显示了 nib 视图。
我在 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.
对我有用
我用自定义框架加载了 nib 视图,但只是在 superView 顶部显示了 nib 视图。
我在 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.
对我有用