UIScrollView and auto layout not working

2019-08-06 07:13发布

I'm trying to make a view scrollable in my app.

O follow exactly the steps in tutorial https://www.youtube.com/watch?v=UnQsFlMGDsI .

The main steps I did were:

  • Add a UIscrollView
  • Add a view into UIscrollView (Content View)
  • Create the margin constraints with value 0 (top, left, right and botton) for UIScrollView
  • Create the margin constraints with value 0 (top, left, right and botton) for Content view
  • Create 2 constraints of "equal width" and "equal height" between ContentView and main view
  • ... (Add some text fields)

When I run the app, the scrollView doesn't work

enter image description here

I really can't make it work. I tried many ways to apply a UIScrollView and didn't get it.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-06 07:31

you can do this for working UIScrollView:

1)add a ViewController

2)put a UIScrollView in it

3)from "Simulated Metric" change size to Freeform. change the height to your desire height

4)add auto layouts to both UIScrollView and View (it's not needed)

5)put your controls in view and add auto layouts to them.

this should work :)

查看更多
Ridiculous、
3楼-- · 2019-08-06 07:36

As mentioned in the video, the main aim is to automatically adjust the UIScrollView contentSize to the size of the screen or to the size of the content in such way that on smaller devices scrolling is active and on larger devices scrolling is not active because it is not needed.

For this to achieve you've to set proper constraints to the content view so that the height is automatically calculated. From your video its visible that you're not setting the proper constraint to the last UITextFeild (this is important).

You have to give the top as well as the bottom constraint to the last UITextField so that contentView height is automatically calculated.

I have done a sample project which you can check here :
Sample Project

查看更多
登录 后发表回答