Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS scrollview ambiguous scrollable content height in case of autolayout

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.

like image 297
nilkash Avatar asked Feb 02 '15 06:02

nilkash


People also ask

What is Contentsize of ScrollView?

The content size of a scroll view doesn't change anything about the bounds of a scroll view and therefore does not impact how a scroll view composites its subviews. Instead, the content size defines the scrollable area. By default, a scroll view's content size is a big, fat {w:0, h:0} .

What is content layout guide in ScrollView?

The most useful here is Content Layout Guide. Basically it helps to determine the scrollable content size of your UIScrollView 's contents. So in your case you have to have a Content view inside your Scroll View that has equal width with Frame Layout Guide and have its edges pinned to Content Layout Guide.

How do I remove content layout guide?

Simply uncheck the Content Layout Guides. This option is found under the Size Inspector tab in storyboard. NOTE: This option is found under the Size Inspector tab in storyboard.


1 Answers

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.

like image 187
Tech_Intelliswift Avatar answered Sep 29 '22 03:09

Tech_Intelliswift