在iPhone上切换应用程序时,更改状态栏颜色的问题 [英] A issue on Changing the Color of the Status Bar when switching apps on iPhone
问题描述
我想使用特定的视图控制器更改状态栏的颜色.
I want to change the Status Bar color with the specific view controller.
根据StackOverFlow的答案,我实现了它.
According to StackOverFlow's answers , i achieved it.
存在一个问题,当在iPhone上切换应用时,我设置的颜色会逐渐消失,并返回到初始状态.
There is an issue , when switching apps on iPhone, the color I have set fades, goes back to initial state.
可以.请注意状态栏.
不正常..请注意状态栏.
我不知道.我尝试的代码:
I can't figure it out. The code I tried:
-
设置
statusBar.backgroundColor
,
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor redColor ];
}
2..将子视图插入statusBar.
2. insert subview to 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).
3. So is to insert layer(CALayer).
我试图用断点对其进行分析.
And I tried to analyze it with breakpoints.
-当应用程序处于活动状态时,双击主页"按钮以切换应用程序,该方法不称为- (void)viewWillDisappear:(BOOL)animated
.这让我有些困惑.
- When the app is a active, and double-click the Home button to Switch apps, the method is not called - (void)viewWillDisappear:(BOOL)animated
. It confuses me a little .
-我尝试在应用程序的方法- (void)applicationWillResignActive:(UIApplication *)application
中更改状态栏的背景颜色,这不起作用.我不知道为什么.
- I try to change the background color of status bar in the Application's method - (void)applicationWillResignActive:(UIApplication *)application
, It doesn't work. I don't know why.
从Github的源代码中,通过运行时就可以了.我的公司不喜欢使用运行时.
While from Github's source code, It's OK through Runtime. My company don't like using Runtime.
还有其他方法没有运行时间吗?
我不知道运行时如何与iPhone的切换应用程序模式交互.
And I don't know how runtime interacts with iPhone's switching apps mode.
主要问题是要在没有运行时的情况下解决.欢迎更多解释.我认为这很容易,我想念什么?
The main question is to solve it without runtime. More explain is welcomed. i think it is easy , what do i miss ?
非常感谢.
推荐答案
Swift 4的答案:
Answer for Swift 4:
它适合由NavigationViewController管理的viewControllers的情况
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
}
}
Objective-C版本的答案:
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];
}
显示操作系统信息的状态栏为 UIWindow ,由应用程序切换器窗口中的操作系统控制.
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"];
因此,在应用切换器窗口中,可以通过更改 View 来调整状态栏位置的背景颜色.
So it's OK to adjust the background color of the status bar position by changing the View , when in the app switcher window.
这篇关于在iPhone上切换应用程序时,更改状态栏颜色的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!