9

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.

Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • In storyboard, select all the labels and make user interaction enable. By default they are disabled. – Ashutosh Jan 09 '14 at 09:53
  • 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 Sep 26 '14 at 16:22

10 Answers10

13

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.

Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • 1
    your solution is still not working here. nothing is scrolling vertically. can you provide a sample project? – itinance Mar 18 '14 at 21:32
  • Regarding the previous commenter (although this is an old answer), make sure that you have 'Use Auto Layout' unchecked in Interface Builder under the 'Identity and Type' section of the View. – AshesToAshes Apr 27 '15 at 18:11
  • Thanks, saved my time ! – Viktor Petrovski Sep 22 '15 at 13:54
  • @Blaszard thanks for your help because in your solution it is mentioned you're not using auto layout which means it is applicable only in that situation. If someone looking for solution with auto layout I solved this problem by following below steps: 1- Add scrollView in storyboard (either in main view or subview) apply auto layout constraints relative to parent and add all subview in scrollView. 2- Now in viewController inside viewDidLayoutSubviews use self.myScrollView.contentSize = CGSizeMake(contentWidth, contentHeight); that's it, it should work now. cheers! – AQEEL AHMED Oct 08 '20 at 06:11
13

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

jaxvy
  • 5,040
  • 1
  • 21
  • 22
10

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.

Mani
  • 17,549
  • 13
  • 79
  • 100
  • I forgot mentioning, but I also tried `CGSizeMake(320, 568)` or `CGSizeMake(320, 1000)` and it wasn't working. – Blaszard Jan 09 '14 at 08:56
  • what is your scrollview height? – Mani Jan 09 '14 at 09:09
  • x = 0, y = 0, width = 320, and height = 568. – Blaszard Jan 09 '14 at 09:11
  • Can You call with a delay function to check what is real scrollview height? on viewDidLoad call this [self performSelector:@selector(doIt) withObject:nil afterDelay:1]; add a function - (void)doIt{NSLog(@"%f", scrollview.frame.size.height)} – Guntis Treulands Jan 09 '14 at 09:13
  • @user2360798 did you enable bounce? Just enable bounce and try to bounce. If it is not working, problem with AutoLayout or autoresize? check and let us know.. – Mani Jan 09 '14 at 09:13
  • Added `self.myScrollView.alwaysBounceVertical = YES;` and was not bouncing at all if I did set it correctly (this is the first time I use scrollview, so don't know what is "the correct" state after setting its property, but it didn't change at all before and after setting its parameter). – Blaszard Jan 09 '14 at 09:18
  • For autoresizing, just look at this question and check it as like.http://stackoverflow.com/questions/7754851/autoresizing-masks-programmatically-vs-interfact-builder-xib-nib – Mani Jan 09 '14 at 09:22
  • sorry now look at this. – Mani Jan 09 '14 at 09:24
  • @user2360798 Did you try this? – Mani Jan 09 '14 at 09:34
  • Thanks. Just solve the issue and posted self-answer. – Blaszard Jan 09 '14 at 10:24
6

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.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
  • @user2360798, Do same with Labels. You window do not cover x = 320 and y = 1500. – Rashad Jan 09 '14 at 09:07
  • I just realized that it was typo. I din't change x and y coordinate of scrollview. Just edited. Sorry. – Blaszard Jan 09 '14 at 09:14
  • As to the labels, I changed x and y, not width and height - otherwise it changes the size of label, not location, if I'm understanding it correctly. – Blaszard Jan 09 '14 at 09:14
  • @user2360798 >> Your problem is not related to Xcode or ios7. Because i am working with scrollview now. I am also using ios7 and Xcode 5. – Rashad Jan 09 '14 at 09:19
  • 1
    @User2360798 Did you solved the problem? I just made a project. If you give you email I can send you. :) – Rashad Jan 09 '14 at 09:34
  • Thanks for your kindness. But I just solved the issue and posted self-answer. Thanks :) – Blaszard Jan 09 '14 at 10:23
5

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.

kkocabiyik
  • 4,246
  • 7
  • 30
  • 40
  • I don't think its right. Because I am using autoLayout and I set the content size in viewDidLoad. Its working just fine. – Rashad Jan 09 '14 at 09:23
3

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

allemattio
  • 1,836
  • 17
  • 33
3

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

Community
  • 1
  • 1
Kirti Nikam
  • 2,166
  • 2
  • 22
  • 43
3

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

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

Chris Trudeau
  • 1,427
  • 3
  • 16
  • 20
vikrant tanwar
  • 219
  • 4
  • 15
2

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.

Blaszard
  • 30,954
  • 51
  • 153
  • 233
fullmoon
  • 8,030
  • 5
  • 43
  • 58
1

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.