0

I have a UiViewController, inside which I have a UIScrollView with the width and height as 768X3000. Inside the UIScrollView I have a view with widht and height as 768X1024.

I am totally confused on how to implement UIScrollView. My scrollView doesnt work. I have lot of contents to be displayed in the UIView and UIScroll doesnt scroll down.

What connections or setting do I have to do make the UIScrollView work.

user2903572
  • 31
  • 1
  • 10
  • myScrollView.contentSize = CGSizeMake(768, 3000); its what you need – Pawan Rai Feb 18 '14 at 18:17
  • I tried this too. It still doesnt work. I believe we have to make the IBOutlet connection to UIScrollView too right? I have made that too. In my viewDidLoad I have written these 3 lines. [self.scroll setDelegate:self]; [self.scroll setScrollEnabled:YES]; self.scroll.contentSize = CGSizeMake(768.0f, 3000.0f); – user2903572 Feb 18 '14 at 19:27

3 Answers3

2

You need to set the contentSize property of your UIScrollView to 768 x 3000, not its frame or its bounds. So in viewWillAppear you could add code to set your scrollView's contentSize:

self.scrollView.contentSize = CGSizeMake(768.0f, 3000.0f);
Steve
  • 1,840
  • 17
  • 20
  • I tried this too. It still doesnt work. I believe we have to make the IBOutlet connection to UIScrollView too right? I have made that too. In my viewDidLoad I have written these 3 lines. [self.scroll setDelegate:self]; [self.scroll setScrollEnabled:YES]; self.scroll.contentSize = CGSizeMake(768.0f, 3000.0f); – user2903572 Feb 18 '14 at 19:24
  • @user2903572 You don't need to set the delegate unless you plan to respond to the delegate method calls. Double-check that scrolling is enabled in Interface Builder by clicking on the scroll view and checking its attributes with the Attributes Inspector. You do have to have an IBOutlet connection if it's set up in Interface Builder. Otherwise you can't set its contentSize. – Steve Feb 18 '14 at 19:37
2

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

-(void)viewDidAppear:(BOOL)animated{

   myScrollView.contentSize = CGSizeMake(768, 3000);

}

2- another tip you can set contentSize in viewDidLayoutSubviews:

3- set the delegate of your scrollview

myScrollView.delegate=self;

//in case you need the delegate methods

4- If still you cannot scroll the view even after you set contentSize correctly, make sure you uncheck "Use AutoLayout" in Interface Builder -> File Inspector. Reference

Community
  • 1
  • 1
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • My scrollView works but I am adding subViews on the parentView. So the scroll works on the first half of the screen and from the where I add the subViews from that portion the scrollView doesn't work. – user2903572 Feb 21 '14 at 17:58
  • You should check the content size of scroll view after adding sub view to it. – Pawan Rai Feb 21 '14 at 21:17
0

If you're using storyboards in Xcode 5 and developing for Auto Layout then there are a few constraints you need for it all to work (in my experience). First, lay your views out like this:

  • main view
    • scroll view
      • content view (put all the stuff you want to scroll in this view)

Then do the following in this order in the storyboard editor:

  1. Set the content view height to 3000. Width should be 768.
  2. Set the scroll view height to 1024. Width should be 768.
  3. Set the main view height to 1024 if in freeform sizing or leave it using inferred sizing. Width should be 768.

Before you do the next steps, just double-check each view's height to be sure nothing changed. Sometimes the storyboard editor makes bizarre changes on its own.

  1. Set a height constraint for the content view only. It should be set at 3000.
  2. Pin the top, bottom, right, and left sides of the content view to the scroll view using 0 for each edge. You will have to manually change the bottom constraint from a negative number to 0. This is very important, so I'll repeat it: manually change the bottom constraint to 0.
  3. Pin the top, bottom, right, and left sides of the scroll view to the main view using 0 for each edge.

Now it should scroll. If you want to ensure that it stays centered when you change to a horizontal orientation, add a horizontal center constraint to the content view as well.

I have many scrolling views in my iPad app and didn't have to use the .contentSize code once if I built my views this way in the storyboard editor.

Good luck! I know what an absolute pain and time-waster this can be.

Kent
  • 1,705
  • 3
  • 16
  • 26
  • My scrollView works but I am adding subViews on the parentView. So the scroll works on the first half of the screen and from the where I add the subViews from that portion the scrollView doesn't work. – user2903572 Feb 21 '14 at 17:58
  • I _think_ that's because you need to set the height constraint for the main subview under the scrollview (that will contain all your other subviews). This is the one I call "content view" above. Be sure to set a single height constraint at 3000. Build a whole new view controller like I explained above and see if it works. Then you can compare to what you've got and find the problem. – Kent Feb 21 '14 at 20:09