横向模式下视频播放后状态栏下的导航栏

Hon*_*ang 25 iphone objective-c ios swift

问题:

在横向模式下播放视频后,导航栏处于状态栏下方.

应用程序:

  • 仅限iOS9.
  • 仅支持纵向模式.
  • 视图控制器上有一个Web视图,Web视图将打开一个youtube链接
  • 视图控制器嵌入在导航控制器中

设置重现:

  1. 在webView中播放视频,
  2. 将设备置于横向模式.
  3. 在横向模式下关闭视频播放,应用程序返回纵向模式
  4. 导航栏位置错误

截图:

  1. 当应用程序打开时

当应用程序打开时

  1. 播放器视频并将设备置于风景中

在此输入图像描述

  1. 问题

在此输入图像描述

Aar*_*ron 22

斯威夫特3

在呈现视图控制器中,覆盖该prefersStatusBarHidden属性以仅隐藏状态栏(如果它在横向中).

override var prefersStatusBarHidden: Bool {
    return UIApplication.shared.statusBarOrientation.isLandscape
}
Run Code Online (Sandbox Code Playgroud)

然后添加一个观察器,以便旋转设备.

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(videoDidRotate), name: .UIDeviceOrientationDidChange, object: nil)
}
Run Code Online (Sandbox Code Playgroud)

在观察者的方法中,调用setNeedsStatusBarAppearanceUpdate:

func videoDidRotate() {
    self.setNeedsStatusBarAppearanceUpdate()
}
Run Code Online (Sandbox Code Playgroud)

应该这样做.

  • 工作几乎完美,但是...当您在视频中轻按“完成”时,同时仍将设备保持在横向,则直到将设备旋转回纵向时,设备才会显示状态栏。 (2认同)

Dan*_*per 11

这很简单,

迅捷3

override func viewWillLayoutSubviews() {
   super.viewWillLayoutSubviews();
   UIApplication.shared.isStatusBarHidden = false
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。这是唯一为我工作的人。 (2认同)
  • UIApplication.shared.isStatusBarHidden 已弃用 (2认同)

Mak*_*ele 8

@Aaron的回答几乎可以解决,只有一个问题:当你点击视频中的"完成",同时仍然以横向方向握住设备时,它将不显示状态栏,直到你将设备旋转回肖像.

在那种情况下,我在点击"完成"按钮时添加了通知观察者,然后我以编程方式切换到肖像.

我的代码在Objective C中:

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidRotate) name:UIDeviceOrientationDidChangeNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closedFullScreen:) name:UIWindowDidBecomeHiddenNotification object:nil];
}

-(void)closedFullScreen:(NSNotification *)myNotification{
    [[UIDevice currentDevice] setValue:
     [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                    forKey:@"orientation"];
}

- (BOOL)prefersStatusBarHidden {
    return UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
}

- (void)videoDidRotate {
    [self setNeedsStatusBarAppearanceUpdate];
}
Run Code Online (Sandbox Code Playgroud)

编辑:

在.plist文件中查看基于控制器的状态栏外观必须设置为YES.