首页 文章

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

提问于
浏览
22

问题:

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

应用程序:

仅限

  • iOS9 .

  • 仅支持纵向模式 .

  • 视图控制器上有一个Web视图,Web视图将打开一个youtube链接

  • 视图控制器嵌入在导航控制器中

设置重现:

  • 在webView中播放视频,

  • 将设备置于横向模式 .

  • 在横向模式下关闭视频播放,应用程序返回纵向模式

  • 导航栏位置错误

截图:

  • 应用程序打开时

when app opens

  • 播放视频并将设备置于横向

enter image description here

  • 问题

enter image description here

4 回答

  • 8

    Swift 3

    在呈现视图控制器中,覆盖 prefersStatusBarHidden 属性以仅在状态栏中隐藏状态栏 .

    override var prefersStatusBarHidden: Bool {
        return UIApplication.shared.statusBarOrientation.isLandscape
    }
    

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

    override func viewDidLoad() {
        super.viewDidLoad()
    
        NotificationCenter.default.addObserver(self, selector: #selector(videoDidRotate), name: .UIDeviceOrientationDidChange, object: nil)
    }
    

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

    func videoDidRotate() {
        self.setNeedsStatusBarAppearanceUpdate()
    }
    

    应该这样做 .

  • 20

    这很简单,

    swift 3

    override func viewWillLayoutSubviews() {
       super.viewWillLayoutSubviews();
       UIApplication.shared.isStatusBarHidden = false
    }
    
  • 2

    @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];
    }
    

    编辑:

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

  • 10

    我尝试了@ Makalele的答案但是并没有完全正常工作(或者它可能会因为其他测试代码而被阻止) . 经过一些测试和尝试,我最终得到了比这更简单的东西 .

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self
                   selector:@selector(setNeedsStatusBarAppearanceUpdate)
                       name:UIDeviceOrientationDidChangeNotification
                     object:nil];
        [center addObserver:self
                   selector:@selector(setNeedsStatusBarAppearanceUpdate)
                       name:UIWindowDidBecomeHiddenNotification
                     object:nil];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    
    - (BOOL)prefersStatusBarHidden {
        return UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    }
    

    有几点需要注意

    • 您可以使用选择器直接呼叫 setNeedsStatusBarAppearanceUpdate .

    • 当视图消失时添加 removeObserver 调用 .

    • 必须不时更改 prefersStatusBarHidden 的返回值 .

    因此,在包含YouTube视图的视图控制器中,您会在进入YouTube全屏之前看到状态栏消失 . 它将在YouTube播放完成后返回(通过 UIWindowDidBecomeHiddenNotification 事件) .

    如果此事件未触发,则另一个事件: UIDeviceOrientationDidChangeNotification ,在用户旋转屏幕时仍会触发(即使方向已锁定) .

    所以,@ Makalele的解决方案有双重触发状态栏 .

    我发现我不需要 UIDevice:setValue:forKey: 但你的里程可能会有所不同 .

    感谢@Makalele和@Aaron .

相关问题