【问题标题】:How to change the status bar background color and text color on iOS 7?如何在 iOS 7 上更改状态栏背景颜色和文本颜色?
【发布时间】:2013-10-04 11:52:49
【问题描述】:

我当前的应用程序在 iOS 5 和 6 上运行。

导航栏为橙色,状态栏为黑色背景色和白色文本颜色。但是,当我在 iOS 7 上运行相同的应用程序时,我观察到状态栏看起来是透明的,与导航栏具有相同的橙色背景颜色,并且状态栏文本颜色为黑色。

因此,我无法区分状态栏和导航栏。

如何使状态栏看起来与 iOS 5 和 6 中的一样,即黑色背景颜色和白色文本颜色?如何以编程方式执行此操作?

【问题讨论】:

标签: ios cocoa-touch ios7 uicolor ios-statusbar


【解决方案1】:

警告:它不再适用于 iOS 13 和 Xcode 11。

================================================ ==========================

我不得不尝试寻找其他方法。这不涉及窗口上的addSubview。因为当键盘出现时我正在向上移动窗口。

目标-C

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

斯威夫特

func setStatusBarBackgroundColor(color: UIColor) {

    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
        return
    }

    statusBar.backgroundColor = color
}

斯威夫特 3

func setStatusBarBackgroundColor(color: UIColor) {

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

    statusBar.backgroundColor = color
}

调用此表单application:didFinishLaunchingWithOptions 对我有用。

注意我们在应用商店中有一个具有这种逻辑的应用。所以我想应用商店政策没问题。


编辑:

使用风险自负。形成评论者@Sebyddd

我有一个应用程序因此被拒绝,而另一个被接受只是 美好的。他们确实认为它是私有 API 使用,因此您需要遵守 审核过程中的运气:) – Sebyddd

【讨论】:

  • 与公认的解决方案不同,这在您改变方向时也有效。谢谢!
  • 这不是私有API使用吗?
  • 我有一个应用程序因此被拒绝,而另一个被接受就好了。他们确实认为这是私有 API 的使用,所以在审查过程中你会受到运气的影响 :)
  • 这个解决方案有一个问题,当你双击home键时,这个状态栏颜色会消失。
  • 不适用于 iOS 13。在 UIApplication 上调用 -statusBar 或 -statusBarWindow 的应用程序:此代码必须更改,因为不再有状态栏或状态栏窗口。在窗口场景中使用 statusBarManager 对象。
【解决方案2】:

转到您的应用程序info.plist

1) 将View controller-based status bar appearance 设置为NO
2) 将Status bar style 设置为UIStatusBarStyleLightContent

然后转到您的应用委托并将以下代码粘贴到您设置 Windows 的 RootViewController 的位置。

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
    view.backgroundColor=[UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}

希望对你有帮助。

【讨论】:

  • 顺便提一下,苹果文档推荐这个 if check 代替:if (NSFoundationVersionNumber
  • @learner 转到 info.plist,然后选择任意行。您将看到一个 + 号。单击加号并从下拉菜单中看到Status bar style 选项。选择它。并粘贴 UIStatusBarStyleLightContent 作为它的值。
  • 这不考虑轮换
  • 最好使用 UIScreen 宽度:UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
  • 更简洁的设置框架的方法是使用UIApplication.sharedApplication().statusBarFrame
【解决方案3】:

1) 在 plist 中将 UIViewControllerBasedStatusBarAppearance 设置为 YES

2) 在 vi​​ewDidLoad 中做一个[self setNeedsStatusBarAppearanceUpdate];

3) 添加如下方法:

 -(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 

更新:
还要检查developers-guide-to-the-ios-7-status-bar

【讨论】:

  • 您可以将其更改为黑色或白色
  • 这没有效果(ios7,模拟器)。 “preferredStatusBarStyle”永远不会被调用。
  • 你在使用 xib 吗?如果是,则更改模拟指标属性中的状态栏值
  • 啊,我发现了问题。 Apple 的 UINavigationController 获取通知 - 即您的答案仅适用于当视图控制器是顶部控制器时,没有容器(没有标签栏,没有导航栏等)。
  • 使用 Storyboard + NavigationController 时的特殊情况。做上面的#1。接下来,为 UINavigationController 创建一个子类(称为 myNavController)。在 Storyboard 中,将 NavigationController 的类设置为“myNavController”。在 myNavController.m 中,执行上面的 #2 和 #3。现在将在您的子类中调用 #3 中的方法(设置日志或断点进行观察)。
【解决方案4】:

在iOS 7中处理状态栏背景颜色时,有2种情况

案例 1:使用导航栏查看

在这种情况下,在您的 viewDidLoad 方法中使用以下代码

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor = [UIColor yellowColor];
 [self.navigationController.navigationBar addSubview:statusBarView];

案例 2:没有导航栏的视图

在这种情况下,在您的 viewDidLoad 方法中使用以下代码

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor  =  [UIColor yellowColor];
 [self.view addSubview:statusBarView];

来源链接http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html

【讨论】:

  • 这对我来说效果很好,但状态栏应该是 20pt 高:[[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 20)];
【解决方案5】:

您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。

extension UIApplication {

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

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}



结果如下:


这是Apple Guidelines/Instruction关于状态栏更改的内容。状态栏中只允许使用深色和浅色(while 和 black)。

这里是 - 如何更改状态栏样式:

如果你想设置状态栏样式,应用程序级别然后在你的`.plist'文件中设置UIViewControllerBasedStatusBarAppearanceNO

如果您想设置状态栏样式,请在视图控制器级别执行以下步骤:

  1. 如果您只需要在 UIViewController 级别设置状态栏样式,请在 .plist 文件中将 UIViewControllerBasedStatusBarAppearance 设置为 YES
  2. 在viewDidLoad添加函数-setNeedsStatusBarAppearanceUpdate

  3. 在您的视图控制器中覆盖 preferredStatusBarStyle。

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

【讨论】:

  • 如果我们使用这些应用程序会被拒绝吗?允许像这样更改 statusBarView 颜色吗?
  • @abhi1992 我不能说苹果是否会接受它,因为我在我的企业应用程序中实现了这个解决方案,不需要在应用商店提交。 :)
  • 如果我把它放在一个基于选项卡的应用程序的视图控制器的 viewdidload 中,它会为每个视图控制器设置颜色,而不仅仅是我放置代码的那个(这正常吗?)跨度>
【解决方案6】:

在 iOS 7 中,状态栏没有背景,因此如果您在其后面放置一个 20 像素高的黑色视图,您将获得与 iOS 6 相同的结果。

此外,您可能还想阅读iOS 7 UI Transition Guide 以了解有关该主题的更多信息。

【讨论】:

  • Gabriele,您能否提供代码如何在其后面放置 20px 高视图?
  • Dejel,这是 Shahid 的回答。
  • 不要只使用“20”!您可以正确获取值,请参阅下面的长答案。
【解决方案7】:

把这个写在你的 ViewDidLoad 方法中:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;
}

它在一定程度上修复了我的状态栏颜色和其他 UI 错位。

【讨论】:

    【解决方案8】:

    这是一个完整的、复制和粘贴的解决方案,带有一个

    绝对正确的解释

    涉及的每个问题。

    感谢Warif Akhand Rishi

    关于 keyPath statusBarWindow.statusBar 的惊人发现。不错。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // handle the iOS bar!
        
        // >>>>>NOTE<<<<<
        // >>>>>NOTE<<<<<
        // >>>>>NOTE<<<<<
        // "Status Bar Style" refers to the >>>>>color of the TEXT<<<<<< of the Apple status bar,
        // it does NOT refer to the background color of the bar. This causes a lot of confusion.
        // >>>>>NOTE<<<<<
        // >>>>>NOTE<<<<<
        // >>>>>NOTE<<<<<
        
        // our app is white, so we want the Apple bar to be white (with, obviously, black writing)
        
        // make the ultimate window of OUR app actually start only BELOW Apple's bar....
        // so, in storyboard, never think about the issue. design to the full height in storyboard.
        let h = UIApplication.shared.statusBarFrame.size.height
        let f = self.window?.frame
        self.window?.frame = CGRect(x: 0, y: h, width: f!.size.width, height: f!.size.height - h)
        
        // next, in your plist be sure to have this: you almost always want this anyway:
        // <key>UIViewControllerBasedStatusBarAppearance</key>
        // <false/>
        
        // next - very simply in the app Target, select "Status Bar Style" to Default.
        // Do nothing in the plist regarding "Status Bar Style" - in modern Xcode, setting
        // the "Status Bar Style" toggle simply sets the plist for you.
        
        // finally, method A:
        // set the bg of the Apple bar to white.  Technique courtesy Warif Akhand Rishi.
        // note: self.window?.clipsToBounds = true-or-false, makes no difference in method A.
        if let sb = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
            sb.backgroundColor = UIColor.white
            // if you prefer a light gray under there...
            //sb.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.9, alpha: 1)
        }
        
        /*
        // if you prefer or if necessary, method B:
        // explicitly actually add a background, in our app, to sit behind the apple bar....
        self.window?.clipsToBounds = false // MUST be false if you use this approach
        let whiteness = UIView()
        whiteness.frame = CGRect(x: 0, y: -h, width: f!.size.width, height: h)
        whiteness.backgroundColor = UIColor.green
        self.window!.addSubview(whiteness)
        */
        
        return true
    }
    

    【讨论】:

      【解决方案9】:

      只是为了添加到 Shahid 的答案 - 您可以使用此 (iOS7+) 考虑方向更改或不同设备:

      - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
        ...
      
        //Create the background
        UIView* statusBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 20)];
        statusBg.backgroundColor = [UIColor colorWithWhite:1 alpha:.7];
      
        //Add the view behind the status bar
        [self.window.rootViewController.view addSubview:statusBg];
      
        //set the constraints to auto-resize
        statusBg.translatesAutoresizingMaskIntoConstraints = NO;
        [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
        [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
        [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
        [statusBg.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[statusBg(==20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(statusBg)]];
        [statusBg.superview setNeedsUpdateConstraints];
        ...
      }
      

      【讨论】:

      • 是的,这样可以更好地处理屏幕尺寸和方向。还要在此代码周围添加类似的内容: if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
      【解决方案10】:

      对于背景,您可以轻松添加视图,例如:

      UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
      view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
      [navbar addSubview:view];
      

      其中“navbar”是一个 UINavigationBar。

      【讨论】:

      • 你的前两行是正确的。但最后一行应该是 [navigationController.view addSubview:view];它应该添加到 UINavigationController 的视图而不是 UINavigationBar 的视图中,因为它将在状态栏不重叠状态栏的 20 px 之后添加视图。
      • 在 Swift 中使用这个: let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIApplication.shared.statusBarFrame.height) let bar = UIView (frame: rect) bar.backgroundColor = UIColor.white navigationController?.view.addSubview(bar)
      【解决方案11】:

      斯威夫特 4:

      // 改变状态栏背景颜色

      let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
      
      statusBar?.backgroundColor = UIColor.red
      

      【讨论】:

        【解决方案12】:

        更改状态栏的背景颜色: 斯威夫特:

        let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))    
                proxyViewForStatusBar.backgroundColor=UIColor.whiteColor()
                self.view.addSubview(proxyViewForStatusBar)
        

        【讨论】:

          【解决方案13】:

          以 iOS 9 上的 swift 2.0 为例

          将以下内容放在应用程序委托中的 didFinishLaunchingWithOptions 下:

              let view: UIView = UIView.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 20))
          
              view.backgroundColor = UIColor.blackColor()  //The colour you want to set
          
              view.alpha = 0.1   //This and the line above is set like this just if you want 
                                    the status bar a darker shade of 
                                    the colour you already have behind it.
          
              self.window!.rootViewController!.view.addSubview(view)
          

          【讨论】:

          • 这行得通,但我认为这不是处理这种风格的最佳方式
          【解决方案14】:

          iTroid23 解决方案对我有用。我错过了 Swift 解决方案。所以也许这会有所帮助:

          1) 在我的 plist 中,我必须添加以下内容:

          <key>UIViewControllerBasedStatusBarAppearance</key>
          <true/>
          

          2) 我不需要调用“setNeedsStatusBarAppearanceUpdate”。

          3) 我必须迅速将其添加到我的 UIViewController:

          override func preferredStatusBarStyle() -> UIStatusBarStyle {
              return UIStatusBarStyle.LightContent
          }
          

          【讨论】:

          • 感谢“UIViewControllerBasedStatusBarAppearance”键,帮助了我:)
          【解决方案15】:

          如果您使用的是UINavigationController,则可以使用这样的扩展名:

          extension UINavigationController {
              private struct AssociatedKeys {
                  static var navigationBarBackgroundViewName = "NavigationBarBackground"
              }
          
              var navigationBarBackgroundView: UIView? {
                  get {
                      return objc_getAssociatedObject(self,
                                                  &AssociatedKeys.navigationBarBackgroundViewName) as? UIView
                  }
                  set(newValue) {
                       objc_setAssociatedObject(self,
                                           &AssociatedKeys.navigationBarBackgroundViewName,
                                           newValue,
                                           .OBJC_ASSOCIATION_RETAIN)
                  }
              }
          
              func setNavigationBar(hidden isHidden: Bool, animated: Bool = false) {
                 if animated {
                     UIView.animate(withDuration: 0.3) {
                         self.navigationBarBackgroundView?.isHidden = isHidden
                     }
                 } else {
                     navigationBarBackgroundView?.isHidden = isHidden
                 }
              }
          
              func setNavigationBarBackground(color: UIColor, includingStatusBar: Bool = true, animated: Bool = false) {
                  navigationBarBackgroundView?.backgroundColor = UIColor.clear
                  navigationBar.backgroundColor = UIColor.clear
                  navigationBar.barTintColor = UIColor.clear
          
                  let setupOperation = {
                      if includingStatusBar {
                          self.navigationBarBackgroundView?.isHidden = false
                          if self.navigationBarBackgroundView == nil {
                              self.setupBackgroundView()
                          }
                          self.navigationBarBackgroundView?.backgroundColor = color
                      } else {
                          self.navigationBarBackgroundView?.isHidden = true
                          self.navigationBar.backgroundColor = color
                      }
                  }
          
                  if animated {
                      UIView.animate(withDuration: 0.3) {
                          setupOperation()
                      }
                  } else {
                      setupOperation()
                  }
              }
          
              private func setupBackgroundView() {
                  var frame = navigationBar.frame
                  frame.origin.y = 0
                  frame.size.height = 64
          
                  navigationBarBackgroundView = UIView(frame: frame)
                  navigationBarBackgroundView?.translatesAutoresizingMaskIntoConstraints = true
                  navigationBarBackgroundView?.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
          
                  navigationBarBackgroundView?.isUserInteractionEnabled = false
          
                  view.insertSubview(navigationBarBackgroundView!, aboveSubview: navigationBar)
              }
          }
          

          它基本上使导航栏背景透明,并使用另一个 UIView 作为背景。您可以调用导航控制器的setNavigationBarBackground 方法来设置导航栏背景颜色和状态栏。

          请记住,当您想要隐藏导航栏时,您必须在扩展程序中使用setNavigationBar(hidden: Bool, animated: Bool) 方法,否则用作背景的视图仍然可见。

          【讨论】:

          • 对我来说这是最好的答案,因为它减轻了许多其他答案问题。缺点是固定的 frame.size.height = 64 这是错误的。最近获取高度的一种方法是 -> .view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0.
          【解决方案16】:

          斯威夫特 4

          Info.plist 添加这个属性

          查看基于控制器的状态栏外观为 NO

          然后在 AppDelegate 里面的 didFinishLaunchingWithOptions 添加这些代码行

          UIApplication.shared.isStatusBarHidden = false
          UIApplication.shared.statusBarStyle = .lightContent
          

          【讨论】:

            【解决方案17】:

            试试这个。 在您的 appdelegate 类 didFinishLaunchingWithOptions 函数中使用此代码:

            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            [application setStatusBarHidden:NO];
            UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
            if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
                statusBar.backgroundColor = [UIColor blackColor];
            }
            

            【讨论】:

              【解决方案18】:

              下面的代码 sn-p 应该适用于 Objective C。

                 if (@available(iOS 13.0, *)) {
                    UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
                    statusBar.backgroundColor = [UIColor whiteColor];
                    [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
                } else {
                    // Fallback on earlier versions
              
                     UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
                        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
                            statusBar.backgroundColor = [UIColor whiteColor];//set whatever color you like
                    }
                }
              

              【讨论】:

              • iOS 13 会出现双状态栏。
              【解决方案19】:

              对于条形颜色:您为条形提供自定义背景图像。

              文字颜色:使用About Text Handling in iOS

              中的信息

              【讨论】:

              • 这不是设置文本颜色
              • 如果您只想设置纯色,则添加背景图像是一种矫枉过正,尤其是如果您希望能够即时更改颜色则不可行
              【解决方案20】:

              我成功自定义了StatusBar颜色,非常简单,在方法中添加AppDelegate.cs文件:

              public override bool FinishedLaunching(UIApplication app, NSDictionary options)

              下一个代码:

              UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
              
              if (statusBar!=null && statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
              {
                 statusBar.BackgroundColor = Color.FromHex(RedColorHex).ToUIColor();
              }
              

              所以你会得到这样的结果:

              链接:https://jorgearamirez.wordpress.com/2016/07/18/lesson-x-effects-for-the-status-bar/

              【讨论】:

              • 这是什么语言?
              • @Alfi Xamarin 在后台形成其 C#
              【解决方案21】:

              在 Swift 5 和 Xcode 10.2 中

              DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
              
              //Set status bar background colour
              let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
              statusBar?.backgroundColor = UIColor.red
              //Set navigation bar subView background colour
                 for view in controller.navigationController?.navigationBar.subviews ?? [] {
                    view.tintColor = UIColor.white
                    view.backgroundColor = UIColor.red
                 }
              })
              

              这里我修复了状态栏背景颜色和导航栏背景颜色。如果您不希望导航栏颜色注释它。

              【讨论】:

                【解决方案22】:

                Swift 代码

                            let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: view.width, height: 20.0))
                            statusBarView.backgroundColor = UIColor.red
                            self.navigationController?.view.addSubview(statusBarView)
                

                【讨论】:

                  【解决方案23】:

                  对于 iOS 13* 和 Swift 4,您可以像下面这样使用。

                  1 -> 将基于视图控制器的状态栏外观设置为 NO

                  extension UIApplication {
                  var statusBarView: UIView? {
                      if #available(iOS 13.0, *) {
                         let statusBar =  UIView()
                  
                          statusBar.frame = UIApplication.shared.statusBarFrame
                  
                          UIApplication.shared.keyWindow?.addSubview(statusBar)
                        
                          return statusBar
                      } else {
                          let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
                          return statusBar
                      }
                  }
                  

                  使用 在 didFinishLaunchingWithOptions 中

                  UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
                  

                  【讨论】:

                    【解决方案24】:

                    使用此扩展程序

                    extension UINavigationController {
                    
                      func setStatusBar(backgroundColor: UIColor) {
                        let statusBarFrame: CGRect
                        if #available(iOS 13.0, *) {
                            statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero
                        } else {
                            statusBarFrame = UIApplication.shared.statusBarFrame
                        }
                        let statusBarView = UIView(frame: statusBarFrame)
                        statusBarView.backgroundColor = backgroundColor
                        view.addSubview(statusBarView)
                      }
                    }
                    

                    【讨论】:

                      【解决方案25】:

                      Xcode 12 +

                      一般情况下,您可以使用 YourProject.xcodeproj 文件更改它 选项卡有更改状态栏颜色的选项 您可以使用此选项设置暗光或默认值 谢谢。

                      【讨论】:

                        猜你喜欢
                        • 2019-11-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2016-10-16
                        • 2015-09-26
                        • 1970-01-01
                        • 2018-04-10
                        • 2022-12-14
                        • 2019-11-20
                        相关资源
                        最近更新 更多