OGeek|极客世界-中国程序员成长平台

标题: ios - 在 iPhone 上切换应用时更改状态栏颜色的问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:16
标题: ios - 在 iPhone 上切换应用时更改状态栏颜色的问题

我想用特定的 View Controller 更改状态栏颜色。

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

一个问题,在 iPhone 上切换应用时,我设置的颜色变淡,回到初始状态。

没关系。请注意状态栏。

enter image description here

不行。请注意状态栏。

enter image description here 我想不通。我试过的代码:

  1. 设置statusBar.backgroundColor,

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

2. 将 subview 插入状态栏。

 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)也是如此。

我尝试用断点分析它。

- 应用处于事件状态时,双击Home键切换应用,方法不调用- (void)viewWillDisappearBOOL)animated 。这让我有点困惑。

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

虽然来自 Github 的源代码,通过 Runtime 就可以了。我的公司不喜欢使用 Runtime

还有其他方法无需运行时吗?

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

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

非常感谢您的进步。



Best Answer-推荐答案


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)viewWillAppearBOOL)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)viewWillDisappearBOOL)animated {
    [self.statusBarBgView removeFromSuperview ];
    [super viewWillDisappear:animated];
}

显示操作系统信息的状态栏,是UIWindow,在应用切换窗口时由操作系统控制。

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

因此,在应用切换器窗口中,可以通过更改 View 来调整状态栏位置的背景颜色。

关于ios - 在 iPhone 上切换应用时更改状态栏颜色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47250495/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4