Vertical scrolling not working in UIScrollView and iOS7 and Xcode 5 despite not using Autolayout

39,486

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 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.

Share:
39,486
Blaszard
Author by

Blaszard

I'm here to gain knowledge and insights on a variety of fields I'm interested in. Specifically, Programming & Software Development (Python and R; no longer use Swift and JavaScript/node.js) Data Science, Machine Learning, AI, & statistics Travel (started in 2016) Language (普通话, français, español, italiano, русский, 한국어) Politics, Economics, and Finance Currently (in 2020), my primary interest is Korean and Russian😈 PS: I'm not a native-English speaker. If you find any errors in my grammar and expressions, don't hesitate to edit it. I'll appreciate it👨🏻‍💼

Updated on July 20, 2022

Comments

  • Blaszard
    Blaszard almost 2 years

    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
    Blaszard over 10 years
    I forgot mentioning, but I also tried CGSizeMake(320, 568) or CGSizeMake(320, 1000) and it wasn't working.
  • Rashad
    Rashad over 10 years
    @user2360798, Do same with Labels. You window do not cover x = 320 and y = 1500.
  • Mani
    Mani over 10 years
    what is your scrollview height?
  • Blaszard
    Blaszard over 10 years
    x = 0, y = 0, width = 320, and height = 568.
  • Guntis Treulands
    Guntis Treulands over 10 years
    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)}
  • Mani
    Mani over 10 years
    @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..
  • Blaszard
    Blaszard over 10 years
    I just realized that it was typo. I din't change x and y coordinate of scrollview. Just edited. Sorry.
  • Blaszard
    Blaszard over 10 years
    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
    Blaszard over 10 years
    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).
  • Rashad
    Rashad over 10 years
    @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.
  • Mani
    Mani over 10 years
    For autoresizing, just look at this question and check it as like.stackoverflow.com/questions/7754851/…
  • Rashad
    Rashad over 10 years
    I don't think its right. Because I am using autoLayout and I set the content size in viewDidLoad. Its working just fine.
  • Mani
    Mani over 10 years
    sorry now look at this.
  • Mani
    Mani over 10 years
    @user2360798 Did you try this?
  • Rashad
    Rashad over 10 years
    @User2360798 Did you solved the problem? I just made a project. If you give you email I can send you. :)
  • Blaszard
    Blaszard over 10 years
    Thanks for your kindness. But I just solved the issue and posted self-answer. Thanks :)
  • Blaszard
    Blaszard over 10 years
    Thanks. Just solve the issue and posted self-answer.
  • itinance
    itinance about 10 years
    your solution is still not working here. nothing is scrolling vertically. can you provide a sample project?
  • AshesToAshes
    AshesToAshes about 9 years
    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.
  • Viktor Petrovski
    Viktor Petrovski over 8 years
    Thanks, saved my time !
  • AQEEL AHMED
    AQEEL AHMED over 3 years
    @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!