9
votes

When I tried to use UIScrollView in iOS 7 and Xcode 5, it doesn't respond to vertical scrolling. I first drag and drop ScrollView from object library to storyboard and set its size as width = 320 and height = 1500, and then drag and drop three labels, and locate those labels at y = 300, 800, 1200. For labels whose y coordinate being equal to 800 and 1200, I first move scrollview to the upper to make those objects located at the corresponding y values. Then, I connect scrollview to implementation file as outlet, and in viewDidLoad method, I write self.myScrollView.contentSize = CGSizeMake(320, 1500). And finally set back the size of scrollview at width = 320 and height = 568. And then run the simulator, but it doesn't react to vertical scrolling. Then I deselected use autolayout and run the simulator again, it's still not working at all.

So how can I fix the issue? Or can anyone inform me of any useful tutorials which teach about UIScrollView in iOS 7 and Xcode 5 (this is important, since every tutorial I've read is based on the prior version)?

I use Xcode 5.0.2.

Thanks.

10
In storyboard, select all the labels and make user interaction enable. By default they are disabled. - Ashutosh
Just for anyone googling here. I had a weird one where, updating to XCode6, for some reason it TURNED OFF (!) "User Interaction Enabled" on a view, in Storyboard. Who knows why! I turned it on and all was well. - Fattie

10 Answers

13
votes

Here's self answer

I changed self.myScrollView.contentSize = CGRectMake(320, 1500); from within viewWillAppear to viewDidLoad, and run the simulator, then for some reasons the scrolling is working now. As I said in the original post, I first wrote it in viewDidLoad and moved it over to viewWillAppear after I read kkocabiyik's answer. I'm not sure then why my original code wasn't working - maybe because I slightly modified my code during me getting lots of helps from Mani and Rashad, but kept the above code within viewWillAppear at the same time.

For those who would be caught in the same trap and come to this question in the future, I've been able to fix the issue from this settings:

  • write self.myScrollView.contentSize = CGSizeMake(320, 1500); within viewDidLoad
  • connect UIScrollView from storyboard to myScrollView as IBOutlet property
  • not using autolayout
  • set x = 0, y = 0, width = 320, height = 568 to scrollview from within storyboard

And also note that using autolayout doesn't work. Also, using autolayout and setting contentsize within viewWillAppear doesn't work, either.

I'd like to appreciate everyone that helps me in this question. Thank you.

13
votes

Take a look at this video. It shows how to use UIScrollLayout and autolayout with XCode 5 / iOS 7.

10
votes

You have to set scrollview height as screensize or less than contentsize. Then only it will scroll vertically.( contensize > scrollview.height) And check bool vairable scrollEnabled set as YES.

6
votes

Set x = 0 and y =0, You have to set the content size to (320,1500), not x and y. x and y defines the starting location of scroll view not the content size. For content size:

    self.scrollView.contentSize = CGSizeMake(320, 1500);

Hope this helps.. :)

5
votes

It doesn't matter whether you modify the frame of any element when you are using AutoLayout. View frames with AutoLayout are handled by it so that It will ignore what you have done on viewDidLoad method. If you must use AutoLayout you may copy paste what you have done in viewDidLoad to viewDidAppear so that It will do all the work done after AutoLayout finishes it work.

3
votes

This tutorial saved my life with scrollviews: Scroll View Autolayout Tutorial

3
votes

I also faced the same issue with Xcode 6. Follow this steps, It is really very helpful and save your time.

3
votes

onOffScroll.contentSize = CGSizeMake(content width,content height);

Use the viewDidLayoutSubviews method and use the code in this method. It may help you.

2
votes

I noticed that scrollview height in storyboard can't be greater than the screen. If you want greater then do it programmatically, not in storyboard.

1
votes

i had a problem where my scroll view would not work when i added a textField or an image. i solved this problem by putting everything into a STACK VIEW. Then when i ran it through my phone i was able to scroll.