3

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?

Storyboard

nirvana002
  • 75
  • 1
  • 7

5 Answers5

3

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

enter image description here

enter image description here

enter image description here

abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
  • First of all thanks for the screenshot explanation. I removed my height constraint of the ContentView (to View) but it changes nothing. I even get the following error in my storyboard: "Scrollview has ambiguous scrollable content height". – nirvana002 May 22 '15 at 10:52
  • I have uploaded the sample code please download and check http://speedy.sh/7XVBk/Sample.zip – abhishekkharwar May 22 '15 at 10:58
  • i added in your project the constraints mentioned in you screenshots. result is identically to my project. Error: "Scrollview has ambiguous scrollable content height" So there must be a hight defined for the contentview? – nirvana002 May 22 '15 at 11:40
  • In this video the contentView height is increased in storyboard during the design process. During my design process all the labels fit on the screen but it is possible that the label height increases dependent on the content. so if i set the height constraint on the contentview, the height is fixed for this view, right? – nirvana002 May 22 '15 at 11:51
  • if your label height will increase then the content height also should get increased(coz its super view of labels) and then the scrollview will scroll. – abhishekkharwar May 22 '15 at 11:53
  • thx, for you help! my mistake was the missing bottom constraint of the last label. – nirvana002 May 22 '15 at 12:35
  • ok great alteast upvote my answer i displayed that you have to set bottom constraint in last label. – abhishekkharwar May 22 '15 at 12:38
  • I set the bottom constraint priority of the last label to 750 and the top constraint priority of the first label to 1000. All the other labels in between have 1000-constraints to each other. Despite this, my top label is not positioned to meet the top constraint. Maybe you know why this is happening? – nirvana002 May 22 '15 at 14:08
  • can you post the screenshot? – abhishekkharwar May 22 '15 at 14:12
  • solved. seems like an xcode bug. after restarting and play with some different color backgrounds it seems to work fine now. crazy day... :-o – nirvana002 May 22 '15 at 14:41
1

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! enter image description here

enter image description here

enter image description here For Actors label constraints include:

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

mars
  • 1,651
  • 2
  • 15
  • 20
0

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
    }
Vizllx
  • 9,135
  • 1
  • 41
  • 79
0

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

Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47
  • Thnx for the testproject. I see that you set the contentview height bigger than the viewcontroller height. In my normal case that shouldn't be the case, only when the labelsize increases dramatically. So there should only be a scrolling if the lables don't fit to the screen size. – nirvana002 May 22 '15 at 12:11
  • thx, for you help! my mistake was the missing bottom constraint of the last label. – nirvana002 May 22 '15 at 12:36
0

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

manman
  • 4,743
  • 3
  • 30
  • 42