关于在 iPhone 上切换应用程序时更改状态栏颜色的问题

A issue on Changing the Color of the Status Bar when switching apps on iPhone

我想用特定的视图控制器更改状态栏颜色。

根据Whosebug的回答,我做到了。

一个问题,当打开应用程序时iPhone,我设置的颜色变淡,回到初始状态。

可以。请注意状态栏。

不正常。请注意状态栏。

我想不通。我试过的代码:

  1. 设置statusBar.backgroundColor,

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
         statusBar.backgroundColor = [UIColor redColor ];
    }
    

2. 插入子视图到 statusBar.

 UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
 UIView * backgroundColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 20) ];
 backgroundColorView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
 backgroundColorView.backgroundColor = [UIColor redColor ];
 [statusBar.subviews.firstObject insertSubview: backgroundColorView atIndex:0];

3. 插入图层(CALayer)也是如此。

而且我试着断点分析。

- 当应用程序处于活动状态时,双击主页按钮切换应用程序时,不会调用该方法 - (void)viewWillDisappear:(BOOL)animated 。这让我有点困惑。

- 我尝试在应用程序的方法- (void)applicationWillResignActive:(UIApplication *)application中更改状态栏的背景颜色,它不起作用。不知道为什么。

虽然从Github的源代码来看,通过运行时是可以的。我的公司不喜欢使用 Runtime

有没有其他方法没有运行时

而且我不知道运行时如何与 iPhone 的切换应用程序模式交互。

主要问题是无需运行时解决。欢迎多多解释。我认为这很容易,我想念什么?

非常感谢。

Swift 4 的答案:

并且符合navigationViewController管理viewController的情况

class ViewController: UIViewController {

    let statusBarBgView = { () -> UIView in
        let statusBarWindow: UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
        let statusBarBgView = UIView(frame: (statusBarWindow.statusBar?.bounds)!)
        return statusBarBgView
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.shared.statusBarStyle = .lightContent
        let navigationBar = self.navigationController?.navigationBar
        self.statusBarBgView.backgroundColor = UIColor.red
        navigationBar?.superview?.insertSubview(self.statusBarBgView, aboveSubview: navigationBar!)
    }


    override func viewWillDisappear(_ animated: Bool) {

        self.statusBarBgView.removeFromSuperview()
        super.viewWillDisappear(animated)
    }

}

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

Objective-C版本的答案:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    self.statusBarBgView = [[UIView alloc ] initWithFrame: statusBar.bounds ];
    self.statusBarBgView.backgroundColor = [UIColor redColor ];
    [navigationBar.superview insertSubview: self.statusBarBgView aboveSubview: navigationBar];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.statusBarBgView removeFromSuperview ];
    [super viewWillDisappear:animated];
}

显示OS信息的状态栏,是UIWindow,在应用程序切换器window中由OS控制。

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

因此可以通过更改 View 来调整 状态栏位置 的背景颜色, 33=].

根据之前的回答修改:

struct Utility {
    static var statusBarBgView = { () -> UIView in
        let statusBarBgView = UIView(frame: UIApplication.shared.statusBarFrame)
        return statusBarBgView
    }()
}

extension UIViewController {

    func setupStatusBar(statusBarBgView: inout UIView) {
        UIApplication.shared.statusBarStyle = .lightContent
        statusBarBgView.backgroundColor = .red
        let navigationBar = self.navigationController?.navigationBar
        UIApplication.shared.keyWindow!.insertSubview(statusBarBgView, aboveSubview: navigationBar!)
    }
}

// Usage:
self.setupStatusBar(statusBarBgView: &Utility.statusBarBgView)