【问题标题】:How to create status bar background in the Navigation Bar without Navigation Bar Titles, items, and more?如何在没有导航栏标题、项目等的导航栏中创建状态栏背景?
【发布时间】:2021-03-03 12:03:39
【问题描述】:

我花了 7 个小时研究编码,但没有任何答案。我相信Apple创建了自己的Apple Map,导航栏中只有一个状态栏区域。我试图寻找这个技巧代码来启用它,就像安全区域指南的绿色背景一样。

如果你知道代码,请告诉我。

谢谢。

【问题讨论】:

    标签: ios swift background navigation uinavigationcontroller


    【解决方案1】:

    如果我理解得很好,您可以使用一个简单的技巧:添加一个 containerView 并在其中执行您的操作,然后将您想要的颜色分配给虚拟状态栏,只需更改视图的背景颜色即可: 声明 containerView 和其中的标签:

    let containerView = UIView()
    let labelInfo = UILabel()
    

    现在在 viewDidLoad 中设置对象和约束:

    view.backgroundColor = .green // change color for status bar color
        
    labelInfo.text = "do your stuff here"
    labelInfo.font = .systemFont(ofSize: 24, weight: .semibold)
    labelInfo.textColor = .black
    labelInfo.textAlignment = .center
    labelInfo.translatesAutoresizingMaskIntoConstraints = false
        
    containerView.backgroundColor = .lightGray
    containerView.translatesAutoresizingMaskIntoConstraints = false
        
    view.addSubview(containerView)
    containerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    containerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    containerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    containerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        
    containerView.addSubview(labelInfo)
    labelInfo.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
    labelInfo.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
    labelInfo.heightAnchor.constraint(equalToConstant: 50).isActive = true
    labelInfo.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    

    这是结果:

    【讨论】:

    • 不错的一个!我可以试试那个方法。在新更新中,我对 Apple Map 和 Snapchat 的一件事是显示模糊背景,状态栏看起来像 Apple 的导航条码。我知道导航栏总是用它来显示顶层,而任何东西都在这样的层之下。
    • @AntonioAdrianChavez 你好,我还没有看到这个更新,你有链接或截图吗?谢谢 :)
    • 我说的不是模糊效果,而是这样的导航栏:stackoverflow.com/questions/52112621/…
    • @AntonioAdrianChavez 但在您的示例链接中是状态栏而不是导航栏...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多