【问题标题】:Change the background color StatusBar based on the current ViewController?根据当前ViewController改变背景颜色StatusBar?
【发布时间】:2017-11-28 10:31:59
【问题描述】:

我希望能够更改我的应用程序的状态栏背景颜色,在 3 个特定的 UIViewControllers 上透明,并将其余部分设置为其他颜色。

我不确定如何检查哪个视图控制器是当前视图控制器。它应该是一个 if/else 语句。我的AppDelegate 中有这个:

extension UIApplication {
    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        let appDelegate = UIApplication.shared.delegate as? AppDelegate

        if let currentVC = appDelegate?.window?.rootViewController?.getCurrentlyDisplayedVC()
        {

            if currentVC is LoginVC || currentVC is RegisterVC || currentVC is LostPasswordVC {
                UIApplication.shared.statusBarView?.backgroundColor = .clear
            } else {
                UIApplication.shared.statusBarView?.backgroundColor = Color_Main_Blue
            }
        }

        UIApplication.shared.statusBarStyle = .lightContent
        return true
    }

【问题讨论】:

标签: ios swift uistatusbar


【解决方案1】:

如果我理解正确,你需要找到当前显示的 viewController。这个扩展应该可以解决问题:

extension UIViewController {
    func getCurrentlyDisplayedVC() -> UIViewController {
        if let presentedVC = presentedViewController {
            return presentedVC.getCurrentlyDisplayedVC()
        } else if let split = self as? UISplitViewController, let last = split.viewControllers.last {
            return last.getCurrentlyDisplayedVC()
        }
        else if let nav = self as? UINavigationController, let top = nav.topViewController {
            return top.getCurrentlyDisplayedVC()
        }
        else if let tab = self as? UITabBarController {
            if let selected = tab.selectedViewController {
                return selected.getCurrentlyDisplayedVC()
            }
        }
        return self
    }
}

然后,你需要在哪里找到当前的 viewController,你调用:

let appDelegate = UIApplication.shared.delegate as? AppDelegate
if let currentVC = appDelegate?.window?.rootViewController?.getCurrentlyDisplayedVC() {
        if currentVC is YourViewController {
            //do what you need
            (UIApplication.shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = UIColor.white
        } else {
            //other checks
        }
    }

【讨论】:

  • 我对其进行了一些更改,但仍然显示蓝色背景颜色没有出现。它适用于登录、注册和丢失密码视图控制器。但它不会改变其余的。这很奇怪。我看不出我做错了什么。请看我上面的更新代码
  • 似乎您仅在应用启动时才调用它。每次更改 vc 时都应该调用它,否则 statusBar 将具有您在开始时设置的背景颜色。
【解决方案2】:

试试下面的代码,可能对你有用。

override func viewWillAppear(animated: Bool) {
    supper.viewWillAppear(animated)

    UIApplication.shared.statusBarHidden = false
    UIApplication.shared.statusBarStyle = .lightContent
    let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    statusBar.backgroundColor = self.view.backgroundColor // or change as you want.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2021-06-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    相关资源
    最近更新 更多