4

Screenshot

My view hierarchy is

UIView

       UIScrollView

           UIView1

               -->UIView1.1

               -->UILabel

               -->UILabel

               -->UILabel

               -->UILabel  (bottom)

I am using Autolayout. I have tried all the ways and searched a lot. I did connect the bottom UILabel to the Bottom Layout of the UIView1 and set its priority to the 750 (the lowest of all). I have almost tried everything which is said on this forum and everywhere because everybody is saying same thing.

I am also adding the one view dynamically in UIView1.1. I have no idea why this is not working. Scrollview is not scrolling properly. Please help me. I am stuck on this for 3 days.

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
Nav
  • 435
  • 1
  • 4
  • 15
  • You need to set the content size maybe this will help you auto set it: CGRect contentRect = CGRectZero; for (UIView *view in self.scrollView.subviews) { contentRect = CGRectUnion(contentRect, view.frame); } self.scrollView.contentSize = contentRect.size; – MrJomp Oct 20 '15 at 17:28
  • @MrJomp Even this is not working. I am adding one view to UIView dynamically. Can that make any difference? – Nav Oct 20 '15 at 17:35
  • Maybe sometimes it helps to NSLog the sizes of the UIViews and the contentSize of the UIScrollView also have you checked you have enabled the vertical and horizontal scrolling of the UIScrollView? If the sizes are ok also try checking other dumb stuff like the "scrollEnabled" and "userInteractionEnabled". NSLog is the key to debugging this problem – MrJomp Oct 20 '15 at 17:45
  • @MrJomp You are right. I am getting content size.height zero. I have no idea why? It is supposed to take the content size from the last constraint I have set for label to the view. But I am getting zero. That is why it is not scrolling. – Nav Oct 20 '15 at 17:53
  • http://stackoverflow.com/questions/12846351/uiscrollview-contentsize-not-working "Put everything in the UIScrollView into another UIView, and put that UIView as the only child of the UIScrollView. Then you can use Auto Layout." – MrJomp Oct 20 '15 at 17:58
  • Already tried like this. – Nav Oct 20 '15 at 18:11

3 Answers3

18

To make this work is actually quite easy. You do not need to put all labels into an extra view. And you do not have to set the contentSize yourself. Auto Layout will do that for you.

You just have to make sure to have the following things:

  1. Each label and the view on top of the labels have to have a width constraint that is set to the width of the scroll view and a left constraint with value 0 (or any padding you might want to add)
  2. The view on top needs a top constraint of value 0
  3. The bottom label needs a bottom constraint of value 0

And that's all!

Here's a sketch to show the constraints:

enter image description here

In case you are using Masonry or SnapKit for your Auto Layout here is how those constraints would be added in code:

topView.snp_makeConstraints { (make) -> Void in
    make.top.equalTo(0)
    make.left.equalTo(0)
    make.width.equalTo(scrollView)
}
label1.snp_makeConstraints { (make) -> Void in
    make.top.equalTo(topView.snp_bottom)
    make.left.equalTo(0)
    make.width.equalTo(scrollView)
}
label2.snp_makeConstraints { (make) -> Void in
   make.top.equalTo(label1.snp_bottom)
   make.left.equalTo(0)
   make.width.equalTo(scrollView)
}
label3.snp_makeConstraints { (make) -> Void in
    make.top.equalTo(label2.snp_bottom)
    make.left.equalTo(0)
    make.width.equalTo(scrollView)
    make.bottom.equalTo(0)
}
joern
  • 27,354
  • 7
  • 90
  • 105
  • This works perfectly for me!, but how add padding to the UIScrollView?, because i've added a UITextField and looks ugly the text fields fit scrollview.width. – Ollie Strevel Jun 13 '19 at 15:47
6
 scrollView.contentSize=CGSizeMake(320,758);
 scrollView.contentInset=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);

Finally these lines saved me.

codercat
  • 22,873
  • 9
  • 61
  • 85
Nav
  • 435
  • 1
  • 4
  • 15
  • 3
    Great that you found a solution. However this solution is not ideal. Because of the fixed sizes this will not work on different screen sizes (like iPhone 5, iPhone 6 and iPhone 6 Plus). It also breaks when the text of your labels and because of that their heights change. Please have a look at my answer for a more robust solution. – joern Oct 20 '15 at 19:46
  • @joern Though you might be right but I already made these lines working dynamically. Moreover, height is not going to change in my case. Thank you for your answer. I'll try this too. – Nav Oct 21 '15 at 02:59
  • 3
    It's good that your solution works for you. However if you are working with Auto Layout, you should benefit from the features it offers you. You really shouldn't calculate the contentSize by yourself. Be lazy, let Auto Layout handle that ;-) – joern Oct 21 '15 at 11:14
0

Working tried out.

if you are not using autolayout then put it only viewDidLoad():

[_mainScroll setContentSize:CGSizeMake(320, 2000)];

or if you are using autolayout then follow this:

First give constrains to scrollview top,bottom,leading and trailing. Now take one more view and give same constrains as scrollview given by you earlier.

Don't forgot to give additional constrain for that view fix height and width equal to main parent view.

or

Just watch this.

https://www.youtube.com/watch?v=rjTS9fyWqdg

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52