为什么contentView在scrollView中有y偏移量

Kes*_*Xie 1 uiscrollview uiview ios swift

我尝试向scrollView添加子视图(UIView)以将内容定位在scrollView中,但我对可滚动内容为何以某个偏移量开始感到困惑.

我的视图层次结构和约束看起来像这样

在此输入图像描述

当我运行应用程序时(滚动视图具有粉红色背景,内容视图具有黑色背景颜色)

在此输入图像描述

在我的故事板中,它看起来像这样

在此输入图像描述

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

override func viewDidAppear(animated: Bool) {
        print(view.frame)
        print(baseView.frame) 
        print(scrollView.frame)
        print(scrollView.bounds)
        print(scrollView.contentSize)
        print(contentView.frame)
    }
Run Code Online (Sandbox Code Playgroud)

我有

  (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)
Run Code Online (Sandbox Code Playgroud)

Sul*_*han 9

请注意,滚动视图位于状态栏和导航栏下方(从...开始y = 0).

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

总的来说64 points.

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

请参阅UIViewController.automaticallyAdjustsScrollViewInsets

您应该将其设置为false您的控制器.