0

How to Attach XIB File at the buttom of Super view

I Have an XIB File Named "xibFIleView"

enter image description here

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:- enter image description here

How Can We Attach XIB view at the Bottom of the super view.

2
  • Did you try solution that I posted? Commented Mar 15, 2019 at 19:03
  • 1
    yes it works for me thanks @Bhaumik Commented Mar 16, 2019 at 11:20

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Use auto layout to add 4 constraints sufficient to specify the subviews width, height, x and y position. For example:

override func viewDidLoad() {
    super.viewDidLoad()

    let child = instanceFromNib()
    self.view.addSubview(child)
    NSLayoutConstraint.activate([
        child.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        child.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        child.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        child.heightAnchor.constraint(equalToConstant: 300) // <- Your desired view height here
    )]
}

func instanceFromNib() -> xibFIleView {

    return UINib(nibName: "xibFileView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! xibFIleView
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.