在ios7中,如何以编程方式隐藏状态栏?我正在使用XCode 4.6.1
(ios6.1)并且我想在 XCode 本身中实现它。
12 回答
在 iOS7 中你应该在你的 viewController 中实现
- (BOOL)prefersStatusBarHidden {
return YES;
}
you can hide status bar to set the key value "View controller-based status bar appearance" NO in plist. This is easiest way.
or You can hide in code by using property statusBarHidden of UIApplication class.
[[UIApplication sharedApplication] setStatusBarHidden:YES];
Swift 3.0
Hide status bar for any particular view controller
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
Hide Status bas across the application
UIApplication.shared.isStatusBarHidden = true
and set the key value "View controller-based status bar appearance" NO in info plist of project.
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
{
[self prefersStatusBarHidden];
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
else
{
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
}
// Add this method
- (BOOL)prefersStatusBarHidden {
return YES;
}
View controller-based status bar appearance
当设置为时隐藏特定的 ViewController(然后重新打开)NO
:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
如果您需要在给定的视图控制器上动态隐藏/显示它,您可以执行类似的操作。
(尽管- (BOOL)prefersStatusBarHidden
如果您不需要更改,我建议您只使用它来返回您的偏好。)
// view controller header
@interface MyViewController : UIViewController {
BOOL shouldHideStatusBar;
}
@end
@implementation
- (BOOL)prefersStatusBarHidden {
return shouldHideStatusBar; // backed by your instance variable
}
- (void)setPrefersStatusBarHidden:(BOOL)hidden {
shouldHideStatusBar = hidden;
// Don't call this on iOS 6 or it will crash since the
// `setNeedsStatusBarAppearanceUpdate` method doesn't exist
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
// [self setNeedsStatusBarAppearanceUpdate]; // (if Xcode 5, use this)
}
@end
如果 iOS >= 7.0,请使用以下代码:
句法:
// Present in UIViewController of UIKit Frameworks
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO
用法:
- (BOOL)prefersStatusBarHidden {
return YES;
}
在 iOS < 7.0 中使用以下代码:
句法:
// Present in UIApplication of UIKit Frameworks
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2);
用法:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
斯威夫特 4.1
步骤1。View controller-based status bar appearance
在你的设置info.plist
为YES
第2步。在你的 中输入一些代码,UIViewController
当你呈现UIViewController
.
private var statusBarIsHidden = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
statusBarIsHidden = true
setNeedsStatusBarAppearanceUpdate()
}
override var prefersStatusBarHidden: Bool {
return statusBarStatus
}
尝试这个
[[UIApplication sharedApplication] setStatusBarHidden:YES];
在迅速 4:
UIApplication.shared.isStatusBarHidden = ..true/false
如果使用 iOS 9.0+ 和 Swift。如果您想在某些视图控制器中隐藏状态栏但不是全部 - 请确保将View controller-based status bar appearance
值Info.plist
设置为YES
否则将在所有视图控制器中使用相同的参数。
prefersStatusBarHidden
并在子类中覆盖UIViewController
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
可以做类似的改变preferredStatusBarStyle
override var preferredStatusBarStyle: UIStatusBarStyle {
get {
return .lightContent
}
}
我的经验是,您需要 iOS 9 / Xcode 7.3 中 info.plist 文件中的代码和值。
将此添加到您的viewDidLoad方法中。
[[UIApplication sharedApplication] setStatusBarHidden:YES];
将此作为布尔值添加到您的 info.plist 文件中,并将其设置为 NO:
View controller-based status bar appearance
如果有人想要最新的方法(Swift 2.1 和最新的 Xcode 7.2)
在 info.plist 中将“基于视图控制器的状态栏外观设置为 NO”
UIApplication.sharedApplication().statusBarHidden = true // 将应用程序委托放入某处(applicationWill 或 DidFinishLaunchingWithOptions: