代码之家  ›  专栏  ›  技术社区  ›  Sandeep Bhandari

即使“edgesForExtendedLayout”设置为“top”,状态栏和toplayout导轨底部锚之间的间隙`

  •  0
  • Sandeep Bhandari  · 技术社区  · 7 年前

    我有一个简单的视图控制器,通过使用

    viewController.present(myVC, animated: true, completion: nil)
    

    myVC视图控制器没有任何嵌入式导航控制器,并且 viewDidLoad 属于 myVC

    下面是如何以编程方式添加视图

                self.view.addSubview(topView)
                topView.translatesAutoresizingMaskIntoConstraints = false
                topView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
                topView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
                topView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive = true
                topView.heightAnchor.constraint(equalToConstant: 70).isActive = true
    

    我已将延伸边指定为我的 属于 myVC

    self.edgesForExtendedLayout = .top
    

    用户界面看起来像

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   joern    7 年前

    获得间隙是因为将topView的顶部约束添加到视图的顶部约束 topLayoutGuide.bottom (位于凹口下方一点)。所以这是有意的行为。

    UINavigationController 使用真正的导航栏。

    topView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: -14).isActive = true
    

    但您必须确保仅在具有safeAreaLayoutGuide.topAnchor>0.(iPhoneX等)。在所有其他设备上,常数必须为 0

    顺便说一句,如果可能的话,你应该改变 self.topLayoutGuide.bottomAnchor (不赞成)到 view.safeAreaLayoutGuide.topAnchor