xcode ios UIScrollView 有一个错误的默认 contentOffset - swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29782712/
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-09-15 07:02:40  来源:igfitidea点击:

ios UIScrollView have a bad default contentOffset - swift

iosxcodeswiftscrollview

提问by Toldy

I have a View inside a ScrollView inside my main View

我的主视图中的 ScrollView 中有一个视图

The problem is that my scrollView have a bad default contentOffset. His value is (0, -64) in portrait

问题是我的 scrollView 有一个错误的默认 contentOffset。他的值是 (0, -64) 在肖像

The Apple doc says

苹果文档说

The default value is CGPointZero.

默认值为 CGPointZero。

I put this code on my controller to temporary handle it :

我将此代码放在我的控制器上以临时处理它:

dispatch_async(dispatch_get_main_queue(), {
  self.scrollView.setContentOffset(CGPointZero, animated:false)
})

Why my contentOffset have not the good default value ?

为什么我的 contentOffset 没有好的默认值?

回答by anasaitali

It's probably an issue with the view insets.

这可能是视图插入的问题。

On your XIB/Storyboard for your view controller, make sure to uncheckthe Adjust Scroll View Insets

在您的视图控制器的 XIB/故事板上,确保取消选中调整滚动视图插入

uncheck the Adjust Scroll View Insets

取消选中调整滚动视图插入

or in your code add self.automaticallyAdjustsScrollViewInsets = false

或在您的代码中添加 self.automaticallyAdjustsScrollViewInsets = false

回答by Adam Smaka

Swift 4.2+

斯威夫特 4.2+

scrollView.contentInsetAdjustmentBehavior = .never

回答by Paul Jarysta

I found the explanation on this website:

我在这个网站上找到了解释:

http://www.codelord.net/2013/10/18/adapting-scroll-views-to-ios-7/

http://www.codelord.net/2013/10/18/adapting-scroll-views-to-ios-7/

Handling navigation bar on top of our scroll view

处理滚动视图顶部的导航栏

The iOS 7 view of course comes with the new look where scroll views go under the navigation bar for a nice effect. One can change the scroll view's contentInset manually to cover for the portion of it that is being overlapped at the top, but doing so manually is tedious and not fun. I was very thrilled to discover handling it should come at no cost if my ViewController has automaticallyAdjustsScrollViewInsets set to YES.

iOS 7 视图当然带有新的外观,滚动视图位于导航栏下方以获得很好的效果。可以手动更改滚动视图的 contentInset 以覆盖它在顶部重叠的部分,但手动执行此操作既乏味又不有趣。我很高兴地发现,如果我的 ViewController 已将自动调整滚动视图插入设置为 YES,则处理它应该是免费的。

This didn't work for me out of the box since it turns out for the magic to happen the scroll view has to be the first subview of your ViewController's UIView. I had to reorder my subviews and then the magic started rolling.

这对我来说开箱即用,因为事实证明,滚动视图必须是您的 ViewController 的 UIView 的第一个子视图。我不得不重新排序我的子视图,然后魔法开始滚动。