UIScrollView + Centered View + Ambigous Scrollable Content Size + many iPhone sizes

前端 未结 3 1976
暗喜
暗喜 2020-12-24 12:43

I have:

  • application, that should work in landscape and portrait mode.
  • view with full-size scroll view on top.
  • some view inside scroll view wi
相关标签:
3条回答
  • 2020-12-24 13:21

    In the morning with a fresh cup of coffee I figured out workaround for this issue!

    So, here is the initial state for simplest case:

    1. scrollView with 0 constraints to all edges
    2. Button centered Horizontal and Vertical with fixed Width and Height
    3. And, of course Has ambiguous scrollable content width and Has ambiguous scrollable content height annoying warnings.

    1

    All, that we have to do is:

    • Add 2 additional constraints, for example "0" for trailing and/or bottom space for our view (in my case - UIButton)

    Important: you have to add trailing and/or bottom constraints. Not "leading and top" - it's not works!

    2

    You can check it in my example project, that demonstrating how to fix this issue: ScrollViewAmbigous

    P.S.

    I don't know why it works and how Xcode detect which constraint is more prioritised (because I'm not set priority for these constraints explicity), but I'll be thankful if someone explain, why it works in comments below.

    0 讨论(0)
  • 2020-12-24 13:34

    Problem :

    1. The warning is telling us that the the content size of the scrollview depends on its subviews.
    2. while your subview don't have any strict rule of position and size (no fixed constraints) it will confuse nib generated scrollview content size.

    Solution :

    1. Make only one subview as a 'contentView' for scrollview.

    2. Add strict (fixed) constraints to that 'contentView'.

      Best practice : equal width and height to its scroll view.

    3. Add all other subviews and constraints to your 'contentView'.

    0 讨论(0)
  • 2020-12-24 13:38

    There seems to be a lot of confusion on this issue. My take is that a UIScrollView must have TWO trailing space constraints, an 'inner' one connecting it to one of it's subviews (so it can know its content width), and another 'outer' one connecting it to a sibling or superview so it knows its frame width.

    Same principle applies for height, i.e. two bottom space constraints.

    0 讨论(0)
提交回复
热议问题