ios - 为什么 contentView 在 scrollView 中有一个 y 偏移量

标签 ios swift uiview uiscrollview

我尝试向 scrollView 添加一个 subview(UIView) 以定位 scrollView 中的内容,但我对为什么可滚动内容以某个偏移量开头感到困惑。

我的 View 层次结构和约束如下所示

enter image description here

当我运行应用程序时( ScrollView 有粉红色背景,内容 View 有黑色背景)

enter image description here

在我的 Storyboard中,它看起来像这样

enter image description here

如果这可能有帮助,在我的 viewDidAppear 方法中:

override func viewDidAppear(animated: Bool) {
        print(view.frame)
        print(baseView.frame) 
        print(scrollView.frame)
        print(scrollView.bounds)
        print(scrollView.contentSize)
        print(contentView.frame)
    }

我得到了

  (0.0, 0.0, 375.0, 618.0)
    (0.0, 64.0, 375.0, 554.0)
    (0.0, 0.0, 375.0, 554.0)
    (0.0, -64.0, 375.0, 554.0) //Why there is a negative y-offset here?
    (375.0, 1200.0)        
    (0.0, 0.0, 375.0, 1200.0)

最佳答案

请注意, ScrollView 位于状态栏和导航栏下方(从 y = 0 开始)。

导航栏的高度 = 44 点 状态栏的高度 = 20 点

总计 64 点

通常 ScrollView 从 y = 0 开始,因此 UIViewController 会自动更改其内容插入,以便内容不会隐藏在顶部栏下。

参见 UIViewController.automaticallyAdjustsScrollViewInsets

您应该在 Controller 上将其设置为 false

关于ios - 为什么 contentView 在 scrollView 中有一个 y 偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36507910/

相关文章:

ios - 如何在 Swift 3/4 中通过单击按钮使图像旋转?

ios - 如何将 Objective C xcode 项目添加到另一个 swift 项目中

ios - Swift 4 - 如何在核心数据中保存 NSAttributedstring

swift - 如何像 Popoverview 一样在 Viewcontroller 上显示 UIViewController

ios - UIView - 带有持续时间的动画似乎忽略了持续时间

ios - 我如何才能知道用户是否拥有纵向模式的 iPhone?

ios - Swift 2.0 中的 UIView.animateWithDuration

ios - 禁止使用 NSLog

ios - 多视口(viewport) OpenGL ES

ios - Xcode 7 Beta 5 - Storyboard 中缺少所有 subview