0
votes

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.

3
myScrollView.contentSize = CGSizeMake(768, 3000); its what you need - Pawan Rai
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

3 Answers

2
votes

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);
2
votes

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

0
votes

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.