我想用特定的视图控制器更改状态栏颜色。
根据StackOverFlow的答案,我实现了它。
有问题,在iPhone上切换应用时,我设置的颜色会消失,会回到初始状态。
没关系。请注意状态栏。
不行。请注意状态栏。
设置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的源代码,通过运行时确定。我的公司不喜欢使用运行时。
还有其他方式没有运行时吗?
而且我不知道运行时如何与iPhone的切换应用模式进行交互。
主要问题是解决它没有运行时。欢迎提供更多解释。我觉得很容易,我想念什么?
非常感谢你的进步。
答案 0 :(得分:2)
Answer for Swift 4:
And it fits the situation of viewControllers managed by 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
}
}
The Answer Of Objective-C version:
-(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];
}
The status bar which shows the OS infos,is UIWindow ,controlled by the OS when in the app switcher window.
UIView *statusBar = [[[UIApplication sharedApplication]
valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
So it's OK to adjust the background color of the status bar position by changing the View , when in the app switcher window.
答案 1 :(得分:0)
根据先前的答案进行了修改:
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)