Xamarin iOS: Auto Layout within scroll view?

前端 未结 2 765
刺人心
刺人心 2020-12-11 12:34

I have a scrollView on my storyboard on which I have added top, left, right, bottom constraints in order for it to resize automatically in accordance with different screen

相关标签:
2条回答
  • 2020-12-11 13:22

    I' ve encountered this question while I was struggling with this problem. The accepted answer is quite right, even if it leaves out some details.

    Here I've made a complete answer will all the details,

    https://stackoverflow.com/a/49096334/3765617

    I write it here because maybe it will be helpful for someone in the future.

    0 讨论(0)
  • 2020-12-11 13:33

    It is a little complicated to explain.

    As we know , before the autolayout get working, we should set constraints on them , according to those constraints the compiler can determine the Frame (position and size) on controls.

    However, UIScrollView is a bit of different from UIView. In general, we just need to set left,right,top,bottom , but in UIScrollView, it has a definition called contentSize which determines the actual size of scrollview. so the constraints we set above is not enough.

    So, as a workaround ,we add a container view inside the scrollview to determine contentSize.

    1. Constraint on scrollview (green area in the following gif)

    2. Constraint on container view.(red area in the following gif)

    pay attention: we set additional width (equal to scrollview) in order to indicate the the width is fixed , means we can't scroll horizontally but vertical orientation is allowed.

    3. Constraint on inner view(blue area in the following gif)

    Test result :

    iPhone5S

    iPad Air2

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