0

I loaded nib view with custom frame but just show that nib view in top of superView.
I call this code in 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)
reza_khalafi
  • 6,230
  • 7
  • 56
  • 82

1 Answers1

0

We can use something like this:

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))

It's works for me in Swift 4.

reza_khalafi
  • 6,230
  • 7
  • 56
  • 82