Search This Blog

Sunday, October 29, 2017

What can go wrong when working with NSLayoutConstraint

Often when working with AutoLayout programmatically something can go wrong. Constraints that you created just not work or program fails. There are most popular mistakes that can happen

1. Forgot to addSubView method

You simply forgot to add your view as subview to parent view.
let containerView = UIView()
self.view.addSubview(containerView)

2. Forgot to set translatesAutoresizingMaskIntoConstraints property to false

Another important thing to remember. You need to enable AutoLayot for your view. So, if you forgot to set translatesAutoresizingMaskIntoConstraints to false then your view will not using AutoLayout.
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false

3. Forgot to activate constraint

When you create new constraint it is not enough to make it work. You have to activate it.
containerView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

4. Your views in different hierarchies

You can only add constraint to views that are in the same hierarchy. In other words - constraint for your view must operate with parent view (or view from the same hierarchy).

5. You must specify position and size both. Directly or indirectly

When you create constraints you need to specify size and position for your view. In other case your constraints will not work.

Directly specifying example (setup center position and width and height)
containerView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 200).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 200).isActive = true

Indirectly specifying example (setup top, bottom, leading and width)
containerView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 25).isActive = true

6 comments:

  1. You wrote an amazing article. I am amazed, your efforts are showing in the article. I hope in future I will see more Articles from you.
    -Custom Website Design

    ReplyDelete
  2. This article impresses me with its well-researched material and good writing. I couldn't put this book down since I was so engrossed in it. Your work and skill have impressed me. Thank you a lot. It can be beneficial to those who want to learn more about Best Custom Websites

    ReplyDelete
  3. Thank you for writing this quality informational content.Your writing technique is impressive and enjoyable to read. I'll come back for more fantastic material.
    Best wishes, and good luck.
    Custom Build Website

    ReplyDelete
  4. This is a great article! Thank you very much. I really need these suggestions. An in-depth explanation is of great benefit. Incredibly inspiring to see how you write. Custom Website

    ReplyDelete
  5. Amazing write-up! The blog was very informative. Keep it up!
    Software Testing Services

    ReplyDelete
  6. Amazing write-up! The blog was very informative. Keep it up!
    Custom Website/a>

    ReplyDelete