代码之家  ›  专栏  ›  技术社区  ›  rantanplan

使用不带状态栏的safeAreaLayoutGuide

  •  1
  • rantanplan  · 技术社区  · 8 年前

    我以编程方式添加所有视图及其自动布局约束。在我的一个视图中,我使用以下方法禁用了状态栏:

    override var prefersStatusBarHidden: Bool {
        return true
    }
    

    显然,这会导致safeAreaLayoutGuide在任何非iPhone X的设备上移动20px。在我的情况下,这会导致iPhone 6/7/8/SE的顶部没有足够的填充物。

    解决这个问题最干净的方法是什么?如果不将填充添加到iPhone X,您将如何添加填充(例如在iPhone 8视图上)?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Lukas Würzburger Chadwick Wood    8 年前

    您可以使状态栏透明,以保留空间,而不是使用 prefersStatusBarHidden . 就间距而言,这对iPhone X没有影响。

    let statusBarWindow = application.value(forKey: "statusBarWindow") as? UIWindow
    statusBarWindow.alpha = 0.0
    

    "statusBarWindow" 实际上是私有API,所以最好像 "status" + "BarWindow" 或者类似的。

        2
  •  2
  •   mugx    8 年前

    我想您可能会使用以下解决方案: 开始创建自定义视图(我们称之为fakeStatusBar),该视图应与状态栏框架匹配,并具有以下约束:

    enter image description here

    请注意,高度=20是不够的,因为状态栏高度取决于您的设备(iPhone 8 plus=20,iPhoneX>20),因此您必须动态更改:

    enter image description here

    self.fakeStatusBarHeight.constant = UIApplication.shared.statusBarFrame.size.height // or whatever you like, considering your padding too
    

    在这一步中,由于fakeStatusBar,我们有了一个很好的空间,然后让我们约束您的内部视图(当然,高度=40只是一个示例):

    enter image description here

    最终结果是:

    iPhoneX

    enter image description here

    iPhone 8 plus:

    enter image description here