1

Inside a UIViewController, I need to have the bottom half scrollable. So I added a UIScrollView and positioned it halfway down the view's height. And in the viewDidAppear method, I have put the below two code lines to make it scrollable.

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;

This way works if the scroll view fills the entire view, I've tested. But this method didn't work for my need. The scroll view would automatically move up and take up the entire screen. I assumed it was the second line of code which causes this.

So I removed the scroll view, added two UIViews to the view controller. To the bottom view, I added the UIScrollView. And in the viewDidAppear method, I have put the same two code lines changing the second line to refer the frame of the UIView that contains the scroll view..

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.containerView.frame;

But it wouldn't scroll either.

Can anyone please tell me how to do this correctly?

Thank you.

Isuru
  • 30,617
  • 60
  • 187
  • 303
  • viewDidAppear seems to be a wrong place for setting frame. Use viewDidLoad instead. Plus `CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);` if the sizes proved are not greater than the scrollView dimension, it won't scroll. Check those. – egghese Jan 30 '14 at 19:55
  • @JeslyVarghese Small progress. Before I moved the code to `viewDidLoad`, it looked like [this](http://i.imgur.com/kyrjm6M.png). But after, it became [this](http://i.imgur.com/uM6c2yk.png). But sadly still it doesn't scroll. I checked the `self.scrollView.frame.size.height` and it's greater than the view's height. – Isuru Jan 30 '14 at 20:01
  • @Isuru Dude, lol don't set absolute values for your content size man. you dont want magic numbers all over your code, it makes your application unreadable for the next programmer. It's best to have it based on the size of the content that's actually inside your scrollview so that your scrollview always scrolls to cover all your content inside it. – Pavan Jan 30 '14 at 20:02
  • @Isuru: I might be very wrong. but turn off auto layout for the time. – egghese Jan 30 '14 at 20:02
  • @Isuru Also, remember your **scrollview will only scroll if the contentsize is greater than the scrollview's frame**. As i made clear in my answer, you dont want absolute values for your contentSize, calculate the contentSize based on the content inside your scrollview by keep tabs of the height as you add content to your scrollview for example. – Pavan Jan 30 '14 at 20:04

1 Answers1

1

Dude, you keep setting the frame of the scrollView to something completely different from what you're actually trying to achieve.

If all you want to do is setup your scroll view so that it only occupies half the space then why dont you just set the frame so that the height only covers the portion of the screen that you want it to cover; and then set the x & y coordinates so that you draw the scroll view from the right position.

Do something like this:

//Shortcut to view's frame.
CGRect viewsFrame = self.view.frame;

/**
CGRectMake takes 4 parameters: x, y, width, height
x: is set to 0 since you want the scrollview to start from the left with no margin
y: you want the y position to start half way, so we grab the view's height and divide by 2  
width: you want your scrollview to span from left to right, so simply grab the view's width
height: you want your scrollview's height to be half of your screen height, so get view's height and divide by 2.
*/
CGRect frameForSV = CGRectMake(0, viewsFrame.size.height/2, viewsFrame.size.width, viewsFrame.size.height/2);

UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:frameForSV];
[self.view addSubview:myScrollView];

Then set your content size not based on an ansolute value, its best to have it based on the size of the content that's actually inside your scrollview so that your scrollview always scrolls to cover all your content inside it.

Also, remember that your scrollview will only scroll if the contentsize is greater than the scrollview's frame

UPDATE 1 after reading your comment in this post simply comment out any code in your viewController.m file related to your scrollview since youre setting up everything in interface builder.

This is the result: enter image description here

Pavan
  • 17,840
  • 8
  • 59
  • 100
  • Thanks for the descriptive answer. Scroll views is my bane in iOS developing. I managed to get it almost working. I don't want to create the scroll view programatically so I did it in IB. Now it scrolls perfectly except for [this](http://i.imgur.com/VpxHVeX.png) issue. As you can see the gap between the above `UIView` and the `UIScrollView`. When it runs, there's a huge gap between them. I have added all the necessary constraints but no luck. Any idea what's causing this issue? – Isuru Jan 30 '14 at 20:16
  • Can you post your xcode project or something so I can have a quick look? – Pavan Jan 30 '14 at 20:17
  • About the content size, the content inside the scroll view won't change. It'll have a fixed height. Do I still have to change it to get the size based on the content? Also can you please show an example on that? – Isuru Jan 30 '14 at 20:17
  • 1
    If it's fixed then cool, no problem :) Normally one would do something like this: http://stackoverflow.com/questions/3046449/calculate-the-contentsize-of-scrollview So they can get a dynamic contentSize should the content every be updated in a scrollView – Pavan Jan 30 '14 at 20:18
  • [Here's](https://www.dropbox.com/s/xeggp9ocpfi6fai/ScrollViewTest.zip) a test project with the issue. – Isuru Jan 30 '14 at 20:20
  • @Isuru remove all programmatic code linked to your scrollview since everything is done on interface builder. – Pavan Jan 30 '14 at 20:20
  • Removing all your code related to your scrollview in your 'viewController.m' file fixes the problem. You will only be left with a very small mergin at the top of your scrollview, thats because of the way you've positioned it in interface builder otherwise that huge gap is gone now. – Pavan Jan 30 '14 at 20:23
  • It fixes the layout problem but it won't scroll now. – Isuru Jan 30 '14 at 20:25
  • @isuru, oh aha so you have more content inside it. Ok cool one moment. Then you simply need to set the contentSize. One moment – Pavan Jan 30 '14 at 20:27
  • @Isuru Just a quick one, can I ask how you plan to populate your scrollview because at the moment it seems as though you've put random views in there just to try and get scrolling to work. If you can tell me what you will actually end up putting in there then that would be great – Pavan Jan 30 '14 at 20:34
  • I'm actually gonna put a single `UIView` with a fixed height (520 to be exact) in it. – Isuru Jan 30 '14 at 20:36
  • @great, thats what I needed to hear. Cool – Pavan Jan 30 '14 at 20:38
  • Yep, I got everything working. I deleted all constraints, I deleted all the junk you had in the scrollview, I also deleted the scrollview, and the extra green view was deleted too. I was left with the bare bones of the viewcontroller, Ie, a view and thats it. Then I added a new scrollview, and progammatically added a UIView with a height of 520. And it was all scrolling nicely. – Pavan Jan 31 '14 at 21:08
  • All in all, it felt that something was wrong with one of your constraints which was causing it to conflict with the scrolling. – Pavan Jan 31 '14 at 21:09