navigation bar under status bar after video playback in landscape mode

前端 未结 4 1735
无人及你
无人及你 2020-12-24 06:58

The problem:

Navigation bar is under status bar after playing video in landscape mode.

The application:

  • iOS9 only.
  • only supports portr
相关标签:
4条回答
  • 2020-12-24 07:09

    Swift 3

    In the presenting view controller, override the prefersStatusBarHidden property to only hide the status bar if it's in landscape.

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

    Then add an observer for when the device is rotated.

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

    In the observer's method, call setNeedsStatusBarAppearanceUpdate:

    func videoDidRotate() {
        self.setNeedsStatusBarAppearanceUpdate()
    }
    

    That should do it.

    0 讨论(0)
  • 2020-12-24 07:09

    It's very simple,

    swift 3

    override func viewWillLayoutSubviews() {
       super.viewWillLayoutSubviews();
       UIApplication.shared.isStatusBarHidden = false
    }
    
    0 讨论(0)
  • 2020-12-24 07:32

    I tried @Makalele's answer but didn't not quite working (or it might but got blocked due to other test code). After some test and try, I end up with something simpler than that.

    - (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);
    }
    

    A few things to notice

    1. You can call setNeedsStatusBarAppearanceUpdate directly using selector.
    2. Added a removeObserver call when view disappears.
    3. The return value of prefersStatusBarHidden must be changed from time-to-time.

    So, in the view controller that contains the YouTube view, you will see the status bar disappears before entering YouTube fullscreen. It will returns when the YouTube playing is completed (via UIWindowDidBecomeHiddenNotification event).

    Just in case that this event doesn't trigger, the other event: UIDeviceOrientationDidChangeNotification, will still trigger whenever user rotates the screen (even the orientation is locked).

    So, @Makalele's solution has double route to trigger status bar.

    I found that I don't need UIDevice:setValue:forKey: but your mileage may vary.

    Credit to @Makalele and @Aaron.

    0 讨论(0)
  • 2020-12-24 07:35

    @Aaron answer almost works, there's only one problem: when you tap "done" in the video, while still holding the device in landscape orientation, it won't show status bar until you'll rotate your device back into portrait.

    In that case, I've added notification observer when "done" button is tapped and then I switch to portrait programmatically.

    My code is in 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];
    }
    

    EDIT:

    View controller-based status bar appearance in .plist file must be set to YES.

    0 讨论(0)
提交回复
热议问题