在我的iOS视频应用中,状态栏隐藏在一些视图控制器中。我已经使用以下代码做到了这一点。

[[UIApplication sharedApplication] setStatusBarHidden:YES];

它适用于iOS 5和iOS 6,但不适用于iOS 7。 我在特定的视图控制器中尝试了这个,

Eg:

-(BOOL)prefersStatusBarHidden { return YES; }

它工作得很好,但是我不能在父视图控制器中再次显示状态栏。


当前回答

iOS 9起:

由于statusBarHidden方法已从iOS9中弃用,您需要在plist中添加如下两个值:

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

请参考以下图片:

statusBarHidden从iOS9被弃用:

@property(重述,非atomic,getter= isstatus barhiden) BOOL 新闻记者:新闻记者 prefersStatusBarHidden”)__TVOS_PROHIBITED;

其他回答

试试这个简单的方法:


objective - c:

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

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated]
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}

迅速:

override func viewWillAppear(animated: Bool) 
{
    super.viewWillAppear(animated)
    UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)
}

override func viewWillDisappear(animated: Bool) 
{
    super.viewWillDisappear(animated)
    UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.None)
}

你应该将这个值添加到plist: "View controller-based status bar appearance"并将其设置为"NO"。

我做了以下操作,它似乎是有效的(即使在iOS 8中):

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {

        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

在iOS10中,我所需要做的就是覆盖我的RootViewController (Swift)中的prefersStatusBarHidden变量:

override var prefersStatusBarHidden: Bool {
    return true
}

在viewdidload中添加以下行

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

并添加新方法

  - (BOOL)prefersStatusBarHidden {
          return YES;
  }

同时更改信息。plist文件 查看基于控制器的状态栏外观" = NO

这对我很有用