0

I have problem that is simply explained but boggles my mind.

I am having an issue changing scrollview content size while simply tapping a button with the following code

if condition {
   scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,  1000 )
   button.setTitle("SHOW LESS", forState: .Normal)
} else {
   scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,  200 )
   button.setTitle("SHOW MORE", forState: .Normal)
}

I am using auto layout, I am aware of the problems that arise with frames not being set yet during the view lifecycle and causing mishaps but this code is executed in an IBAcition. The strange thing is, if the content size is greater than 200 to start, it will shrink. It just never grows to 1000. My only guess is that it has to do with the constraints linked to the button which are nothing special, just centered and pinned vertically but I would have no idea where to start debugging that. Also when I remove the set title call, it DOES grow to 1000! I can't for the life of me figure out why setting the title would mess with content size. Any ideas?

Greg Price
  • 2,556
  • 1
  • 24
  • 33
  • Can you explain a little bit more what you are trying to achieve? Why are you changing the `contentSize` and not the actual size of the scroll view? When working with Auto Layout you should not really set the `contentSize` manually. Maybe a screenshot would help. – joern Oct 22 '15 at 20:26
  • So the overall functionality being implemented is hide and show content within a scrollview which require the content size to be recalculated fairly frequently... Is there a preferred approach using auto layout? I have looked into some of the UILabel resizing stuff and apple docs on auto layout and scrollview and haven't found a great solution yet – Greg Price Oct 22 '15 at 21:05
  • Looks like this is a fairly common problem. I found a solution here which is in line with that you guys are saying. There is just weird behavior with auto layout and contentSize http://stackoverflow.com/questions/12846351/uiscrollview-contentsize-not-working – Greg Price Oct 23 '15 at 15:39

1 Answers1

1

The cleanest way to go about this would probably be to never calculate the contentSize yourself. When you add all the views to you UIScrollview, the constraints they have should determine the contentSize unambiguously. Then, the UIScrollView will automatically adapt its size.

When you need to change some size/margin etc. in code, you should probably create an IBOutlet to the constraint and change the constraint.constant. The ScrollView will automatically then adapt the contentSize.

Now there is one tricky thing: The ScrollView will not decrease the contentSize, if that means that a view in the ScrollView becomes invisible. (I assume when showLess is called, you are moving something out of the ScrollView's contentView) What you can to about this is create a Constraint that sets the spacing between the view and the bottom of the ScrollView. Then you can set this spacing to a negative value (so it moves below the scrollView) and set the view to hidden. I think that would solve your problem.

JWKot
  • 360
  • 3
  • 11
  • Ahh interesting Ill give this a go and let you know. I understand what you mean however about adding views that are all relative inside a scrollview... Unfortunately in this case Im implementing hide and show behavior hence the complex recalculations of contentSize – Greg Price Oct 22 '15 at 21:02