anik_s anik_s - 3 months ago 10
iOS Question

Auto Layout constraint is not maintained when changing the height of the view

I am fairly new to iOS app design. I have a container view (let's say conView) and a Label (let's say nameLabel) on a storyboard ViewController. I have set the top space constraint for nameLabel to be 20 point below conView. Now, when I changed the height for conView programmatically, the top space constraint for nameLabel is not maintained properly. I have used this code to change the height.

conView.frame.size.height = 200

What am I missing here? What will be the correct way to achieve the desired output?


Changing the frame of a view breaks the layout constraints and forces the view to change the dimensions or position. What you have to do is update the constant of the constraint instead of changing the frame of the container view.

Let's say you have a constraint defining the height of the container view. Bind the constraint to the view controller(constHeight)

constHeight.constant = 200

If you do not have a height constraint defined for container view, update the available constraints to achieve the height you need.

This will update the height constraint. now we have to tell it that the constraints have been changed and it should update the layouts.


Hope this will help.