【问题标题】:ios - Back ground color of status bar turns white while using scroll viewios - 使用滚动视图时状态栏的背景颜色变为白色
【发布时间】:2018-01-24 05:33:10
【问题描述】:

我是新手,正在尝试实现滚动视图。当我使用滚动视图而不嵌入导航控制器时,状态栏背景颜色不会改变,如我所愿。

但是当我嵌入导航控制器时,状态栏的背景颜色变为白色。

当我嵌入导航控制器时,如何防止状态将其颜色变为白色。谢谢。

这里是约束。 Constraints

【问题讨论】:

  • 请添加屏幕截图
  • 是的,我已经添加了。点击好的和坏的文字。
  • 试试这个答案可能会有帮助
  • @Krunal 是的,分享了它。检查此链接以获取更多参考。 youtube.com/watch?v=LhhW3xqhCzg&t=182s

标签: ios swift uinavigationcontroller background-color statusbar


【解决方案1】:

查看基于控制器的状态栏外观 = False/True

此外,还有一种可编程的方法。如果你想要的话,请告诉我

【讨论】:

  • 我尝试在 info.plist 中使用。结果是一样的。你也可以告诉我可编程的方式。
  • 是的,我将不得不深入挖掘我的代码
  • 覆盖 public var preferredStatusBarStyle: UIStatusBarStyle { if DefinedConstants.isiPad() { return .default } return .lightContent }
【解决方案2】:

在导航控制器之后试试这个:

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        if statusBar.responds(to: Selector(("setBackgroundColor"))) {
            statusBar.backgroundColor = UIColor.magento
        }

func setStatusBarBackgroundColor(color: UIColor) {

        guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
        statusBar.backgroundColor = color
    }

【讨论】:

  • 我在 viewdidload 方法中添加了这段代码,它显示了这个错误“调用中的参数标签不正确(有 'hex:',预期的 'coder:')”
  • 你必须使用简单的 UIColor.magenta
  • 或者使用这个 UIColor.init(red: , green: , blue: , alpha: ) 并在之前使用的字段中填写颜色代码
  • 如果有帮助请点赞
  • 是的。但我不清楚这个解决方案。如果您查看我使用的这个 5 分钟教程链接,那么您可以轻松地帮助我。 youtube.com/watch?v=LhhW3xqhCzg&t=180s
【解决方案3】:

这是因为状态栏与您嵌入的导航栏共享相同的背景。在您的主视图中,在viewDidLoad() 下添加以下代码并根据需要设置背景颜色:

    self.navigationController?.hidesBarsOnSwipe = true

    /* creates a View using the navigation bar frame to place under the status bar but above the navigation bar */

    let statusBarView = UIView()
    statusBarView.backgroundColor = .blue
    statusBarView.frame = UIApplication.shared.statusBarFrame
    UIApplication.shared.keyWindow?.addSubview(statusBarView)

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 2018-04-10
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多