6

I am using Auto Layout in my iPhone app and have a UIScrollView. I need to change the content size of my scroll view at several points while my apps running (so setting the content size in viewWillAppear is useless as I have seen this suggested in other places).

When I change it, my subviews jump about, presumably because it breaks the auto layout constraints.

So how can I approach changing my scrollview content size with auto layout enabled?

Thanks.

sunkehappy
  • 8,970
  • 5
  • 44
  • 65
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
  • Try it in viewDidAppear instead of in viewWillAppear – sunkehappy Nov 18 '12 at 12:35
  • Sorry if I wasn't clear. I of course set the content size initially when the view loads/appears, but I also need to change it multiple times in the same view after it loads/appears in my own methods. This is my issue. – Josh Kahane Nov 18 '12 at 19:01
  • 1. Maybe your view is too complicated for the auto layout. 2. You have not specified the correct auto layout constraints, try some other constraints. 3. Use code in viewWillLayoutSubviews to layout your subview manually. – sunkehappy Nov 19 '12 at 03:05
  • did you solve the problem ? – Mouhamad Lamaa Jan 16 '13 at 08:18
  • 1
    @JoshKahane - did you get a chance to try viewDidLayoutSubviews? – d2burke Jan 21 '13 at 14:15

2 Answers2

14

I was having this same issue, and I know this can't be the final solution, but for now, rather than calling this in viewWillLayoutSubviews, by putting the code inside viewDidLayoutSubviews it allowed me to set the contentSize AFTER the viewController did it's default business :)

Hope that helps.

d2burke
  • 4,081
  • 3
  • 39
  • 51
0

Test your code in viewWillLayoutSubviews. Apple say:

When a view’s bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.

UIViewController Class Reference

sunkehappy
  • 8,970
  • 5
  • 44
  • 65