【问题标题】:Swift - Is it possible to set a specific Status Bar color for ALL view controllers?Swift - 是否可以为所有视图控制器设置特定的状态栏颜色?
【发布时间】:2016-12-22 13:46:52
【问题描述】:

我搜索和搜索,到目前为止我只找到了以下答案:

  1. 为特定视图控制器选择特定颜色
  2. 根据当前的视图控制器使状态栏颜色改变

我需要知道:是否可以为应用程序中随处出现的状态栏确定通用颜色?

感谢您的帮助

【问题讨论】:

  • 是的,有可能
  • 是的,你可以简单地在你的根控制器上设置它。
  • 您正在寻找的是:[[UINavigationBar appearance] setBarTintColor:__YOUR_COLOR__]; 吗?它为您的应用程序(所有视图控制器)设置状态栏的色调。
  • 你不能给它上色。默认情况下,它的外观类似于 lightContent 等。

标签: ios swift colors universal uistatusbar


【解决方案1】:
  1. 设置“基于视图控制器的状态栏外观”
  2. (UIViewControllerBasedStatusBarAppearance) 在 Info.plist 中设置为“是”。 (YES 是默认值,因此您也可以将此值保留在 plist 之外。)
  3. 在您的viewDidLoad 方法中,调用[self setNeedsStatusBarAppearanceUpdate]
  4. 实现preferredStatusBarStyle,返回此视图控制器所需的状态栏样式:

    - (UIStatusBarStyle) preferredStatusBarStyle { 
        return UIStatusBarStyleLightContent; 
    }
    

【讨论】:

  • 我真的不确定,但这不是让每个 ViewController 显示自己的状态栏颜色的方法吗?我写了(第 1 号)我已经找到了。我希望能够只定义一次。不过还是谢谢
【解决方案2】:

实现这一点的方法是创建一个具有适当位置和尺寸的 UIView,然后直接将其添加到 UIWindow:

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var statusBarColorView: UIView?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
window?.makeKeyAndVisible()

    let v = UIView(frame: CGRect(x: 0, y: 0, width: window?.frame.size.width ?? 1024, height: 20))
    v.backgroundColor = UIColor.white
    v.translatesAutoresizingMaskIntoConstraints = false
    window?.addSubview(v)
    window?.addConstraints(NSLayoutConstraint.constraints(
        withVisualFormat: "V:|-0-[v(==20)]",
        options: [],
        metrics: nil,
        views: ["v": v]))
    window?.addConstraints(NSLayoutConstraint.constraints(
        withVisualFormat: "H:|-0-[v]-0-|",
        options: [],
        metrics: nil,
        views: ["v": v]))

    return true
}

这种方法的问题是您需要自己根据设备方向/当前显示的视图控制器隐藏/显示它。

更改颜色将如下所示:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.statusBarColorView?.backgroundColor = UIColor.blue
    }
}

您肯定想根据您的颜色自定义 UIStatusBarStyle,您可以通过在 Info.plist 中为键 UIViewControllerBasedStatusBarAppearance 选择 YES 来实现此目的

【讨论】:

    【解决方案3】:

    编辑所以,感谢 HAS,我明白这不是一个好的答案。它可以工作,但它使用私有 API(使用 KVC 访问 statusBarWindow 和 statusBar),因此它可能不会与 Apple 一起使用。这不是答案:)

    好吧,经过长时间的研究,我找到了方法,而且非常简单。 再次,我试图给状态栏一个 custom 颜色,如黑色、白色或橙色。实现这一点的方法是:

    1. 转到 Info.plist 并添加(如果还没有的话)“查看基于控制器的状态栏外观”并将其设置为:NO
    2. 转到项目的部署信息并将“状态栏样式”设置为“浅色”或“默认”(取决于您选择的颜色。对于黑色,您应该选择“浅色”)
    3. 转到 AppDelegate.swift 并添加以下函数:

      func setStatusBarBackgroundColor(color: UIColor){
          guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
              return
          }
      
          statusBar.backgroundColor = color
      }
      
    4. 在 AppDelegate.swift 中,转到这个函数:

      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
      

      并在其中调用我们在 3 上创建的函数,意思是: setStatusBarBackgroundColor(TheColorYouWant)

    就是这样 :) 它适用于所有视图,无需编写更多代码。 如果我犯了任何错误,或者如果有办法以任何方式改进代码,请随时纠正我。

    非常感谢那些花时间提供帮助的人

    【讨论】:

    • 这使用私有 API,可能不会让您将应用提交到 AppStore。
    • statusBarWindowstatusBar 都是私有的。否则它们会列在这里 developer.apple.com/library/ios/documentation/UIKit/Reference/… 并且你不需要使用 KVC 来获取这些属性。
    • 好吧,我不能说我不相信。感谢你告诉我。那么不可能以适当的、非私有的 API 方式实现这样的事情吗?我有点绝望:P
    • 我不知道有什么办法:(
    【解决方案4】:

    您也可以添加应用代理:

    - (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    …
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    …
    }
    

    或者我在第一条评论中讨论的另一种方式:

    1. 在 .plist 文件中将 UIViewControllerBasedStatusBarAppearance 设置为 YES。
    2. viewDidLoad 做一个[self setNeedsStatusBarAppearanceUpdate];
    3. 添加如下方法:

      - (UIStatusBarStyle)preferredStatusBarStyle
      { 
          return UIStatusBarStyleLightContent; 
      }
      

    注意:如果您有导航栏,这将不起作用。

    【讨论】:

      猜你喜欢
      • 2018-05-24
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      相关资源
      最近更新 更多