我希望状态栏显示在viewWillAppear()中并在viewController的viewWillDisappear()中消失
我正在使用
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
但在iOS 9.0中已弃用
我正在使用
UIApplication.shared.isStatusBarHidden = false
在swift中,但在目标C中,这只是值......
prefersStatusBarHidden对我来说也不起作用,因为我需要在viewWillDisappear()函数中隐藏状态栏
-(BOOL)prefersStatusBarHidden{
return YES;
}
任何人都可以帮助我吗?
答案 0 :(得分:4)
对于每个视图控制器,您想要更改状态栏的可见性,您需要覆盖prefersStatusBarHidden
。为此,实际上工作,您必须在项目的Info.plist
中添加以下键/值:
<强>键:强>
View controller-based status bar appearance
值: YES
要控制viewWillAppear
和viewWillDisappear
中状态栏的可见性,您可以执行以下操作:
var statusBarHidden = false
override func viewWillAppear() {
super.viewWillAppear()
statusBarHidden = false
self.setNeedsStatusBarAppearanceUpdate()
}
override func viewWillDisappear() {
super.viewWillDisappear()
statusBarHidden = true
self.setNeedsStatusBarAppearanceUpdate()
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
答案 1 :(得分:0)
对于Swift 3,
override var prefersStatusBarHidden: Bool{
return true
}
并添加viewDidLoad()
self.modalPresentationCapturesStatusBarAppearance = true
答案 2 :(得分:0)
为Objective-C编写示例(@dennykim已经为SWIFT编写了相同的代码)
@property(nonatomic,assign)BOOL statusBarHidden;
-(void)viewWillAppear:(BOOL)动画{
[super viewWillAppear:animated];
self.statusBarHidden = TRUE;
[self setNeedsStatusBarAppearanceUpdate];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.statusBarHidden = FALSE;
[self setNeedsStatusBarAppearanceUpdate];
}
- (BOOL)prefersStatusBarHidden {
return self.statusBarHidden;
}