How to Attach XIB File at the buttom of Super view
I Have an XIB File Named "xibFIleView"
My code for calling XIB View is:-
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(instanceFromNib())
}
func instanceFromNib() -> xibFIleView {
return UINib(nibName: "xibFileView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! xibFIleView
}
}
When I run My project my Simulator shows:-
How Can We Attach XIB view at the Bottom of the super view.
You can achieve this by setting constraints
or frame
to your xibView
.
Set Constraints:
override func viewDidLoad() {
super.viewDidLoad()
let xibView = instanceFromNib()
self.view.addSubview(xibView)
xibView.translatesAutoresizingMaskIntoConstraints = false
let constraint_leading = NSLayoutConstraint(item: xibView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
let constraint_bottom = NSLayoutConstraint(item: xibView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0)
let constraint_height = NSLayoutConstraint(item: xibView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: xibView.frame.height)
let constraint_width = NSLayoutConstraint(item: xibView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: xibView.frame.width)
self.view.addConstraint(constraint_leading)
self.view.addConstraint(constraint_bottom)
xibView.addConstraint(constraint_height)
xibView.addConstraint(constraint_width)
}
----- or -----
Set frame:
Make following changes in your viewDidLoad()
:
override func viewDidLoad() {
super.viewDidLoad()
let xibView = instanceFromNib()
let y_pos = self.view.frame.height - xibView.frame.height
xibView.frame = CGRect(x: 0, y: y_pos, width: xibView.frame.width, height: xibView.frame.height)
// change x, y, width, height based on your requirement.
self.view.addSubview(xibView)
}
Note: change x, y position and width, height based on your requirement.