UIScrollview with Auto-Layout

2019-07-04 07:07发布

问题:

I'm struggling with UIScrollView for a while now.

"descriptionLabel" and "genresLabel" will increase height during runtime and the gap to actors will increase. So i need the scrolling functionality here to see the actors label.

I set the height of the contentView as a height-constraint equal to View with priority 250 and hoped that the height will increase automatically if needed.

In fact nothing happens and no scrolling is possible.

How can i automatically increase the contentView height in the auto-layout context?

回答1:

You have to make ContentView's height flexible so it should get increased and the scrollview's content size will increase accordingly. Have a look into attached screenshots. Increase the bottom label top constraint (which is 495) value the scrollview's content size will get increased automatically.

Here is the Sample Source Code



回答2:

Is your Actors label, the last element on this View? If it is then fix both Actors: and asdfas... to top, bottom and left. Now, for bottom set 'Constant' as 'Use standard value'.Also fix topmost element(here maybe 'No network connection!') to top and set its 'Constant' as 'Use standard value'. Try this and let me know if it works!

I have tried what i have told you.Pasting images of the same. Hope it helps!

For Actors label constraints include:

and for asdfas label constrains are as shown in the following image:



回答3:

Just check this link, just have to write this to add up content view height:-

override func viewDidLayoutSubviews()
    {
        let scrollViewBounds = scrollView.bounds
        let containerViewBounds = contentView.bounds

        var scrollViewInsets = UIEdgeInsetsZero
        scrollViewInsets.top = scrollViewBounds.size.height/2.0;
        scrollViewInsets.top -= contentView.bounds.size.height/2.0;

        scrollViewInsets.bottom = scrollViewBounds.size.height/2.0
        scrollViewInsets.bottom -= contentView.bounds.size.height/2.0;
        scrollViewInsets.bottom += 1

        scrollView.contentInset = scrollViewInsets
    }


回答4:

It's hard to understand whats wrong with your layout, so I've created test project with a 2 growing labels, all works fine, maybe you will find out what's wrong in your constraints :)

https://www.dropbox.com/s/j8269c03b32gra5/scrollview-test.zip?dl=0

Build my project and click "More text" button to add text dynamically to labels in scrollview



回答5:

For me, I needed to add a bottom constraint to my last element in the page. More explained here