ios 尽管不使用自动布局,但在 UIScrollView 和 iOS7 和 Xcode 5 中垂直滚动不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21015490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:03:07  来源:igfitidea点击:

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

iosobjective-cxcodeuiscrollview

提问by Blaszard

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 viewDidLoadmethod, 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 autolayoutand run the simulator again, it's still not working at all.

当我尝试在 iOS 7 和 Xcode 5 中使用 UIScrollView 时,它不响应垂直滚动。我首先将对象库中的 ScrollView 拖放到 storyboard 并将其大小设置为 width = 320 和 height = 1500,然后拖放三个标签,并将这些标签定位在 y = 300、800、1200 处。 对于 y 坐标的标签等于 800 和 1200,我首先将滚动视图移到上方以使这些对象位于相应的 y 值处。然后,我将滚动视图作为插座连接到实现文件,并在viewDidLoad方法中编写self.myScrollView.contentSize = CGSizeMake(320, 1500). 最后将 scrollview 的大小设置为 width = 320 和 height = 568。然后运行模拟器,但它对垂直滚动没有反应。然后我取消选择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.

谢谢。

回答by Blaszard

Here's self answer

这是自己的答案

I changed self.myScrollView.contentSize = CGRectMake(320, 1500);from within viewWillAppearto 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 viewDidLoadand moved it over to viewWillAppearafter 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 viewWillAppearat the same time.

self.myScrollView.contentSize = CGRectMake(320, 1500);从内部更改viewWillAppearviewDidLoad,然后运行模拟器,然后由于某些原因滚动现在可以工作了。正如我在原帖中所说的那样,我首先写了它,viewDidLoad然后viewWillAppear在阅读了 kkocabiyik 的答案后将其移到了。我不确定为什么我的原始代码不起作用 - 可能是因为我在从 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);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
  • self.myScrollView.contentSize = CGSizeMake(320, 1500);在里面viewDidLoad
  • 将 UIScrollView 从故事板连接到 myScrollView 作为 IBOutlet 属性
  • 不使用自动布局
  • 将 x = 0, y = 0, width = 320, height = 568 设置为从故事板中滚动视图

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

还要注意,使用自动布局不起作用。此外,使用自动布局并在其中设置 contentsizeviewWillAppear也不起作用。

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

我要感谢所有在这个问题上帮助我的人。谢谢你。

回答by jaxvy

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

看看这个视频。它展示了如何在 XCode 5 / iOS 7 中使用 UIScrollLayout 和自动布局。

回答by Mani

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 scrollEnabledset as YES.

您必须设置scrollview height as screensize or less than contentsize.Then 只有它会垂直滚动。( contensize > scrollview.height)并检查 bool 变量scrollEnabled设置为YES.

回答by Rashad

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:

设置 x = 0 和 y =0,您必须将内容大小设置为 (320,1500),而不是 x 和 y。x 和 y 定义滚动视图的起始位置而不是内容大小。对于内容大小:

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

Hope this helps.. :)

希望这可以帮助.. :)

回答by kkocabiyik

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 viewDidLoadmethod. If you mustuse AutoLayout you may copy paste what you have done in viewDidLoadto viewDidAppearso that It will do all the work done after AutoLayout finishes it work.

在使用 AutoLayout 时,是否修改任何元素的框架都没有关系。带有 AutoLayout 的视图框架由它处理,因此它会忽略您在viewDidLoad方法上所做的事情。如果您必须使用 AutoLayout,您可以将您所做的复制粘贴viewDidLoad到其中,viewDidAppear以便它会在 AutoLayout 完成工作后完成所有工作。

回答by allemattio

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

本教程使用滚动视图挽救了我的生命: 滚动视图自动布局教程

回答by Kirti Nikam

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

我在 Xcode 6 上也遇到了同样的问题。按照以下步骤操作,它真的非常有帮助,可以节省您的时间。

回答by vikrant tanwar

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

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

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

使用该viewDidLayoutSubviews方法并使用该方法中的代码。它可能会帮助你。

回答by Badr

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.

我注意到故事板中的滚动视图高度不能大于屏幕。如果你想要更大,那么以编程方式进行,而不是在故事板中。

回答by Badr

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.

我有一个问题,当我添加一个 textField 或一个图像时,我的滚动视图不起作用。我通过将所有内容放入堆栈视图来解决这个问题。然后当我通过手机运行它时,我能够滚动。