简体   繁体   English

如何隐藏iOS 7.1中的状态栏?

[英]How can I hide the status bar in iOS 7.1?

In iOS 7.0, I hid the status bar in my apps by adding 在iOS 7.0中,我通过添加隐藏了我的应用中的状态栏

<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

to the info.plist. 到info.plist。 I just updated my testing iPad to iOS 7.1, and the status bar is now back in all of my apps. 我刚刚将测试iPad更新到iOS 7.1,状态栏现在又回到了我的所有应用程序中。 How can I hide it in both 7.0 and 7.1? 如何在7.0和7.1中隐藏它?

Update: This is only happening in iPhone apps running on the iPad, I don't see this problem on the iPhone or in the simulator. 更新:这只发生在iPad上运行的iPhone应用程序中,我没有在iPhone或模拟器中看到这个问题。

In the view controllers in which you want the status bar hidden, add the following method 在您希望状态栏隐藏的视图控制器中,添加以下方法

- (BOOL)preferStatusBarHidden {
  return YES;
}

Then you can call 然后你可以打电话

[self setNeedsStatusBarAppearanceUpdate];

which will fire the change to the status bar. 这会将更改激活到状态栏。 This call can be done inside of an animation block which will animate the change. 此调用可以在动画块内完成,动画块将为更改设置动画。

Try adding the following 尝试添加以下内容

   - (void)viewWillAppear:(BOOL)animated{
        NSLog(@"View will appear");
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

    }

    - (void)viewWillDisappear:(BOOL)animated{

        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    }

I can reproduce the problem with a single-view iPhone-only app running in iPhone-compatibility mode in the simulator. 我可以在模拟器中以iPhone兼容模式运行的单视图iPhone专用应用程序重现此问题。 But only when selecting an iPad non-retina on iOS 7.1. 但只有在iOS 7.1上选择iPad非视网膜时。

My findings: 我的发现:

  • The status bar will not be hidden, whatever you specified in the plist or in code. 无论您在plist中还是在代码中指定,状态栏都不会被隐藏。
  • The problem doesn't occur on retina iPads 视网膜iPad上不会出现此问题
  • The problem doesn't occur on iOS 7 or iOS 6 iOS 7或iOS 6上不会出现此问题

I tried these keys in the .plist: 我在.plist中尝试了这些键:

<key>UIStatusBarHidden</key>
<true/>
<key>UIStatusBarHidden~ipad</key>
<true/>

and

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIViewControllerBasedStatusBarAppearance~ipad</key>
<false/>

I also tried the ViewController based-solution as mentioned by @Irfan to no avail. 我也试过@Irfan提到的基于ViewController的解决方案无济于事。

There also seems no way to detect if the statusbar is shown as [UIApplication sharedApplication].statusBarFrame returns {0, 0, 0, 0} 似乎也无法检测状态栏是否显示为[UIApplication sharedApplication] .statusBarFrame返回{0,0,0,0}

Add this to ViewDidLoad: 将其添加到ViewDidLoad:

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

And Implement below Method: 并实施以下方法:

- (BOOL)prefersStatusBarHidden {
return YES;
 }

It will hide status-bar of that particular ViewController in which you Implement it. 它将隐藏您实现它的特定ViewController的状态栏。 It works superbly good for me. 它对我来说非常好。 Hopefully it will also help you out. 希望它也会帮助你。

The only solution I have found to this is to add the following: 我发现的唯一解决方案是添加以下内容:

- (UIStatusBarStyle) preferredStatusBarStyle {
    return -1;
}

wherever you have: 无论你在哪里:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

This is obviously terrible, but it seems to work for me -- at least so far. 这显然是可怕的,但它似乎对我有用 - 至少到目前为止。

UPDATE: I have noticed this causing output like the following: 更新:我注意到这导致输出如下:

<Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

I find another workaround, and it's possible that this error is what makes this workaround work, so I'm sticking with it, but it's worth noting. 我找到了另一种解决方法,这个错误可能会使这个解决方法起作用,所以我坚持使用它,但值得注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM