Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView Not Scrolling?

I have a UIScrollView created in interface builder with a bunch of UITextView and UIImageView objects already added. I have it connected to an IBOutlet, and under -(void)viewDidLoad I set its content size to 1200x2000. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?

like image 732
67cherries Avatar asked Nov 04 '13 15:11

67cherries


4 Answers

  1. viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.

  2. Are you using autolayout? If so, check out the following answer (and forget 1.): UIScrollView doesn't use autolayout constraints.

like image 158
Nenad M Avatar answered Oct 17 '22 17:10

Nenad M


The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:

  1. Leave autolayout enabled.
  2. Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
  3. Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
like image 37
Randel S Avatar answered Oct 17 '22 16:10

Randel S


viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.

like image 43
Cezar Avatar answered Oct 17 '22 17:10

Cezar


If you're using Autolayout, make sure you put the code to set Content size within

viewDidLayoutSubviews
like image 32
nithinreddy Avatar answered Oct 17 '22 17:10

nithinreddy