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 a
You should follow the approach below. First of all, here are some important things about scroll Views which are important for auto layout:
UIScrollView
changes its bounds automatically.UIScrollView
needs a content View(in UI) for getting content size for scrolling which works smoothly for auto layout.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.
UIScrollView
, connect TOP and BOTTOM constraints to TOP & Bottom Layout Guide and LEADING and TRAILING to the Main View.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.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.