ios setStatusBarHidden 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18303746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:47:27  来源:igfitidea点击:

setStatusBarHidden not working

ios

提问by Rose Perrone

In my UIViewController, I have:

在我的UIViewController,我有:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [self.view sizeToFit];
}

Yet the view looks like this:

然而,视图看起来是这样的:

enter image description here

在此处输入图片说明

I'm sure this code runs. I load the view from a xib. I haven't done anything else to the status bar like change its style. What could be wrong?

我确定这段代码可以运行。我从xib. 我没有对状态栏做任何其他事情,比如改变它的风格。可能有什么问题?

Even when I set `application.statusBarHidden = YES" in my app delegate, I see:

即使我在我的应用程序委托中设置了“application.statusBarHidden = YES”,我也会看到:

enter image description here

在此处输入图片说明

回答by Rose Perrone

In your app's plist, if you have "View controller-based status bar appearance" set to YES, put this code in the view controller in which you hide the status bar:

在您的应用程序的 plist 中,如果您将“基于视图控制器的状态栏外观”设置为“是”,请将此代码放在隐藏状态栏的视图控制器中:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

Else if "View controller-based status bar appearance" is set to NO, call the following whenever you want to hide the status bar.

否则,如果“查看基于控制器的状态栏外观”设置为 NO,则在您想隐藏状态栏时调用以下代码。

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

回答by Erhan Demirci

if you want to hide Status Bar in your app , Follow this steps :

如果你想在你的应用中隐藏状态栏,请按照以下步骤操作:

Step 1 :

第1步 :

enter image description here

在此处输入图片说明

Step 2:

第2步:

enter image description here

在此处输入图片说明

Step 3:

第 3 步:

Add to your appDelegate didFinishLaunchingWithOptions function

添加到您的 appDelegate didFinishLaunchingWithOptions 函数

application.statusBarHidden = YES;

so :

所以 :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      application.statusBarHidden = YES;
}

回答by Marcelo Fabri

That's because iOS 7 has changed the way it deals with the status bar.

那是因为 iOS 7 改变了它处理状态栏的方式。

Setting UIViewControllerBasedStatusBarAppearanceto NOon your app Info.plistshould work.

在您的应用程序上设置UIViewControllerBasedStatusBarAppearance为应该可以工作。NOInfo.plist

回答by Jouhar

You can show/hide your app status bar using the following code (Works on IOS 7 - IOS 8 and IOS 9):

您可以使用以下代码显示/隐藏您的应用程序状态栏(适用于 IOS 7 - IOS 8 和 IOS 9):

in your project .h file add this boolean:

在您的项目 .h 文件中添加这个布尔值:

BOOL isShowStatus;

And in .m file add this:

在 .m 文件中添加:

//To show the status bar:
-(void)showTheStatusBar
{
    isShowStatus = YES;
    [self setNeedsStatusBarAppearanceUpdate];
}

//And to hide the status bar:
-(void)hideTheStatusBar
{
    isShowStatus = NO;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (BOOL)prefersStatusBarHidden {
    return !isShowStatus;
}

Simply call it from anywhere, didload for example:

只需从任何地方调用它,例如 didload:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //To show the status bar:

    [self showTheStatusBar];

    //Or to hide it:

    [self hideTheStatusBar];
}

回答by Javier Calatrava Llavería

For me it works fine:

对我来说它工作正常:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

ALWAYS in the root view. If you are doing that in a subview will not work because status bar visibility will be taken from parent view.

始终在根视图中。如果您在子视图中这样做将不起作用,因为状态栏的可见性将从父视图中获取。

回答by Abdullah Shafique

Try adding this after you hide the status bar:

隐藏状态栏后尝试添加:

 [self.view setFrame:[self.view bounds]];

In your appdelegate.m in didFinishLaunchingWithOptions:

在您的 appdelegate.m 中didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      application.statusBarHidden = YES;
}

When I run your code:

当我运行你的代码时:

enter image description here

在此处输入图片说明