7

Hi I am developing small IOS application in which I am using scrollview with auto-layout.Inside scroll I am adding two more views. I am using IB and auto-layout constraints. I am adding two views in side one after another in vertical manner. I have added outer constraints like trailing, leading, top, bottom space. I also added height constraints for both views. Till this everything is working fine.

But my view1 has some dynamic content. For that reason I want to make height constraint greater than equal to instead of equal to.

enter image description here

enter image description here

So how to resolve this problem. Need some help. Thank you.

2 Answers 2

22

You should follow the approach below. First of all, here are some important things about scroll Views which are important for auto layout:

  1. UIScrollView changes its bounds automatically.
  2. UIScrollView needs a content View(in UI) for getting content size for scrolling which works smoothly for auto layout.
  3. UIScrollView's top and bottom constraint should connected to top and bottom layout guide (For most of the cases, not all).

As per your problem:

First Approach: You have UIScrollView, so just insert one UIView inside it and consider it as Content View. After that put your two UIViews inside the UIView (Content View).

So the Hierarchy is: MainView --> UIScollView --> UIView (ContentView) --> firstView & Second View. Now we are going to give constraints to all of them.

  • For UIScrollView, connect TOP and BOTTOM constraints to TOP & Bottom Layout Guide and LEADING and TRAILING to the Main View.
  • For UIView (Content View) it is very important to give constraints LEADING, TRAILING, TOP, BOTTOM to the UIScrollView and to give the explicit height (normal height constraint) to your contentView which is appropriate for scrolling (e.g 1200). Also make it horizontally center in container.
  • Now give constraints to your first view: LEADING, TRAILING, TOP to ContentView and give explicit height (normal height constraint). Don't try to modify it in greater than equal right now - we will do this later. Then, give constraint to second view Leading, Trailing to ContentView, Top to FirstView, Bottom to Content View and Explicit Height (what ever you want). Now, try to modify the Height constraint of first view from equal -> greater than equal - it will definitely work.

Second Approach (Easy and Simple): After giving constraint as per first point, for changing height of first View dynamically you can create an IBOutlet of height constraint of first view to your class and as per your requirement you can change the constraint's constant value (as per you want the height of first view) in any method or button action so it will change in run time. You can also consider it is a fine trick when you want to hide your views so just change their Height constraint's constant to 0 so it will hide and at the time of unhide, again set the constant value of same to desired value so you can also easily play with hide and unhide functionality of view which is little bit difficult in auto layout from other ways.

0
0

I would make this constraint as type equals and give it low priority. Then, during adding dynamic content, you may just add another constraint with higher priority.

If you load this UIView from xib just make sure you provide constraint with higher priority.

Not the answer you're looking for? Browse other questions tagged or ask your own question.