iOS控制状态栏的显示和隐藏

做了一个图片、视频浏览功能,需要在浏览时隐藏状态栏,视频、图片全屏显示出来。
如果单纯的是在导航push的viewController中进行显示,那状态栏很容易控制。

View controller-based status bar appearance  设置为 YES

在viewController中添加如下控制方法

#pragma mark - 图片视频的浏览隐藏状态栏
- (BOOL)prefersStatusBarHidden {
    return self.statusBarHidden;
}

- (void)refreshStatusBarHidden:(NSNotification *)notification {
    
    NSDictionary *dict = notification.userInfo;
    self.statusBarHidden = [dict[@"isshow"] boolValue];
/// 强制刷新状态栏
    [self setNeedsStatusBarAppearanceUpdate];
    
}

那么如果是presentViewController呢?
分两种情况,
1、

modalPresentationStyle = UIModalPresentationFullScreen;

与上面的设置方法一致。
2、没有设置UIModalPresentationFullScreen,实现了自定义动画效果transitioningDelegate
这个时候用上面的方法设置是行不通的。因为状态栏还是归负责present你的vc的nav视图所有。如果你想在presentViewController目标vc上执行状态栏的控制,需要添加

/// 可以接管状态栏
modalPresentationStyle = UIModalPresentationCustom;

另外,如果你的项目设置

View controller-based status bar appearance  设置为 NO

就可以用下面的方法控制了。

[UIApplication sharedApplication].statusBarHidden = YES;

你可能感兴趣的:(iOS控制状态栏的显示和隐藏)