简体   繁体   English

如何在iOS 11上的iPhone X上获得黑色状态栏

[英]How to get a black statusbar on iPhone X on iOS 11

By default the statusbar on an iPhone X looks like this: 默认情况下,iPhone X上的状态栏如下所示:

在此输入图像描述

But I would like to achieve this: 但我想实现这个目标:

在此输入图像描述

I tried setting the preferredStatusBarStyle to lightContent but it only worked after setting the background behind the statusbar to black. 我尝试将preferredStatusBarStyle设置为lightContent但它只在将状态栏后面的背景设置为黑色后才起作用。

To fix the rounded corners I ended up adding another subview with rounded corners. 为了修复圆角,我最后添加了另一个圆角的子视图。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .black

        let roundedView = UIView(
            frame: CGRect(
                x: 0,
                y: UIApplication.shared.statusBarFrame.height,
                width: view.frame.width,
                height: view.frame.height
            )
        )
        roundedView.layer.cornerRadius = 10
        roundedView.layer.masksToBounds = true
        roundedView.backgroundColor = .white
        view.addSubview(roundedView)

        let label = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 60))
        label.text = "Black statusbar!"
        label.textAlignment = .center
        roundedView.addSubview(label)
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

I'm wondering if this is the best approach.. there must be a better way to achieve this. 我想知道这是否是最好的方法..必须有更好的方法来实现这一目标。


UPDATE UPDATE

This is a terrible idea because: 这是一个可怕的想法,因为:

Don't mask or call special attention to key display features. 请勿掩盖或特别注意按键显示功能。 Don't attempt to hide the device's rounded corners, sensor housing, or indicator for accessing the Home screen by placing black bars at the top and bottom of the screen. 不要试图通过在屏幕的顶部和底部放置黑条来隐藏设备的圆角,传感器外壳或用于访问主屏幕的指示器。 Don't use visual adornments like brackets, bezels, shapes, or instructional text to call special attention to these areas either. 不要使用括号,边框,形状或说明文字等视觉装饰来特别注意这些区域。

  • The rounded corners at the top of the view might look nice, but you will have to add exceptions in the code to make sure the rounded corners are not shown on other iPhones. 视图顶部的圆角可能看起来不错,但您必须在代码中添加例外以确保其他iPhone上不显示圆角。 You will need to put this in all your ViewControllers / Storyboards.. That is not so great. 你需要把它放在你所有的ViewControllers / Storyboard中。这不是那么好。

  • The rounded corners at the bottom of the view will appear straight in screenshots, but the corners at the top (set manually) won't. 视图底部的圆角将在屏幕截图中显示为直线,但顶部的角(手动设置)不会。 This will be ugly when users share your app. 当用户分享您的应用时,这将是丑陋的。

Safe Area Layout is a solution to your problem. 安全区域布局是您的问题的解决方案。

I tried following the solution in my existing projects and it works fine. 我尝试在现有项目中使用该解决方案并且工作正常。

  • Set black background color (or what ever you want) to main view of your view controller. 将黑色背景颜色(或任何您想要的颜色)设置为视图控制器的主视图。
  • Now add child view (with white background) and set its constraints relative to Safe Area Layout (or follow Top and bottom layout guide if Xcode version is below 9). 现在添加子视图(带有白色背景)并相对于安全区域布局设置其约束(如果Xcode版本低于9,则按照顶部和底部布局指南)。
  • Consider this child view (with white background) as a main view and process further design with it. 将此子视图(带有白色背景)视为主视图,并使用它进一步设计。

Here is sample snapshot with result, By enabling or disabling Safe Area layout, I tested and implemented. 以下是带有结果的示例快照,通过启用或禁用安全区布局,我进行了测试和实施。

FYI: In these snapshots, main view has red background and child view has blue background color. 仅供参考:在这些快照中,主视图具有红色背景,子视图具有蓝色背景颜色。

Safe Area Layout: 安全区域布局: 在此输入图像描述

AutoLayout 自动版式

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM