IOS scrollview ambiguous scrollable content height in case of autolayout

前端 未结 2 1686
逝去的感伤
逝去的感伤 2021-02-04 17:26

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

相关标签:
2条回答
  • 2021-02-04 17:55

    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.

    0 讨论(0)
  • 2021-02-04 17:56

    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)
提交回复
热议问题