gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:22:03 24 4
gpt4 key购买 nike

我想用特定的 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 respondsToSelector:@selector(setBackgroundColor:)]) {
    statusBar.backgroundColor = [UIColor redColor ];
    }

2. 将 subview 插入 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 。这让我有点困惑。

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

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

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

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

主要问题是在没有运行时的情况下解决它。欢迎多多解释。我认为这很容易,我想念什么?

非常感谢。

最佳答案

Swift 4 的答案:

而且很适合viewControllers被navigationViewController管理的情况

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];
}

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

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com