简体   繁体   English

尽管没有使用Autolayout,但垂直滚动在UIScrollView和iOS7以及Xcode 5中不起作用

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

When I tried to use UIScrollView in iOS 7 and Xcode 5, it doesn't respond to vertical scrolling. 当我尝试在iOS 7和Xcode 5中使用UIScrollView时,它不响应垂直滚动。 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. 我首先将ScrollView从对象库拖放到storyboard,并将其大小设置为width = 320和height = 1500,然后拖放三个标签,并在y = 300,800,1200处找到这些标签。对于y坐标的标签等于800和1200,我首先将scrollview移动到上部,使这些对象位于相应的y值。 Then, I connect scrollview to implementation file as outlet, and in viewDidLoad method, I write self.myScrollView.contentSize = CGSizeMake(320, 1500) . 然后,我将scrollview连接到实现文件作为outlet,在viewDidLoad方法中,我写了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. 最后在width = 320和height = 568处设置scrollview的大小。然后运行模拟器,但它不会对垂直滚动做出反应。 Then I deselected use autolayout and run the simulator again, it's still not working at all. 然后我取消选择use autolayout并再次运行模拟器,它仍然没有工作。

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)? 或者任何人都可以告诉我任何有用的教程,这些教程讲授iOS 7和Xcode 5中的UIScrollView(这很重要,因为我读过的每个教程都是基于以前的版本)?

I use Xcode 5.0.2. 我使用Xcode 5.0.2。

Thanks. 谢谢。

Here's self answer 这是自我回答

I changed self.myScrollView.contentSize = CGRectMake(320, 1500); 我改变了self.myScrollView.contentSize = CGRectMake(320, 1500); from within viewWillAppear to viewDidLoad , and run the simulator, then for some reasons the scrolling is working now. viewWillAppearviewDidLoad ,并运行模拟器,然后由于某些原因滚动现在正在工作。 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. 正如我在原帖中所说,我首先在viewDidLoad写了它,并在我读完kkocabiyik的答案后将其移到了viewWillAppear 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. 我不确定为什么我的原始代码不起作用 - 也许是因为我在从Mani和Rashad获得大量帮助的过程中稍微修改了我的代码,但同时在viewWillAppear中保留了上述代码。

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); self.myScrollView.contentSize = CGSizeMake(320, 1500); within viewDidLoad viewDidLoad
  • connect UIScrollView from storyboard to myScrollView as IBOutlet property 将故事板中的UIScrollView作为IBOutlet属性连接到myScrollView
  • not using autolayout 不使用自动布局
  • set x = 0, y = 0, width = 320, height = 568 to scrollview from within storyboard 设置x = 0,y = 0,width = 320,height = 568以从故事板内滚动查看

And also note that using autolayout doesn't work. 并且还要注意使用autolayout不起作用。 Also, using autolayout and setting contentsize within viewWillAppear doesn't work, either. 此外,在viewWillAppear中使用autolayout和设置contentsize也不起作用。

I'd like to appreciate everyone that helps me in this question. 我想感谢所有帮助我解决这个问题的人。 Thank you. 谢谢。

Take a look at this video. 看看这个视频。 It shows how to use UIScrollLayout and autolayout with XCode 5 / iOS 7. 它显示了如何在XCode 5 / iOS 7中使用UIScrollLayout和autolayout。

You have to set scrollview height as screensize or less than contentsize. 您必须将scrollview height as screensize or less than contentsize.设置为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 . (contensize> scrollview.height)并检查bool vairable scrollEnabled设置为YES

Set x = 0 and y =0, You have to set the content size to (320,1500), not x and y. 设置x = 0和y = 0,您必须将内容大小设置为(320,1500),而不是x和y。 x and y defines the starting location of scroll view not the content size. x和y定义滚动视图的起始位置而不是内容大小。 For content size: 对于内容大小:

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

Hope this helps.. :) 希望这可以帮助.. :)

It doesn't matter whether you modify the frame of any element when you are using AutoLayout. 使用AutoLayout时是否修改任何元素的框架无关紧要。 View frames with AutoLayout are handled by it so that It will ignore what you have done on viewDidLoad method. 具有AutoLayout的查看框架由它处理,因此它将忽略您在viewDidLoad方法上所做的操作。 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. 如果你必须使用AutoLayout,你可以将你在viewDidLoad完成的工作粘贴到viewDidAppear ,这样它将完成AutoLayout完成工作后完成的所有工作。

本教程使用scrollviews保存了我的生活: Scroll View Autolayout Tutorial

我也遇到了与Xcode 6相同的问题。按照这个步骤 ,这非常有帮助,节省您的时间。

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

Use the viewDidLayoutSubviews method and use the code in this method. 使用viewDidLayoutSubviews方法并使用此方法中的代码。 It may help you. 它可能会帮助你。

I noticed that scrollview height in storyboard can't be greater than the screen. 我注意到故事板中的scrollview高度不能大于屏幕。 If you want greater then do it programmatically, not in storyboard. 如果你想要更大,那么以编程方式,而不是在故事板中。

i had a problem where my scroll view would not work when i added a textField or an image. 我有一个问题,当我添加textField或图像时,我的滚动视图不起作用。 i solved this problem by putting everything into a STACK VIEW. 我通过将所有内容放入堆叠视图来解决这个问题。 Then when i ran it through my phone i was able to scroll. 然后,当我通过手机运行时,我能够滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM