martin-cotta
4/11/2016 - 11:38 PM

Hack to fix navigation bar position/height on iOS 8 after closing fullscreen video

Hack to fix navigation bar position/height on iOS 8 after closing fullscreen video

// on a portrait only app, when a video player is launched from a WebView
// and the user turn the phone into landscape and then closes the player,
// the navigation bar slips under the status bar

@property (nonatomic, strong) id observer;

- (instancetype)init {

  @weakify(self);
  
  NSString *name = @"UIWindowDidRotateNotification";
  void(^block)(NSNotification * _Nonnull note) = ^void(NSNotification * _Nonnull note) {
    @strongify(self);
    if([note.userInfo[@"UIWindowOldOrientationUserInfoKey"] intValue] >= 3) { // old orientation was landscape
      [self.navigationController setNavigationBarHidden:YES animated:NO];
      [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
  };
  
  // or 
  
  NSString *name = UIDeviceOrientationDidChangeNotification;
  void(^block)(NSNotification * _Nonnull note) = ^void(NSNotification * _Nonnull note) {
    @strongify(self);
    self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
  };

  // add observer for notification  
  _observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:nil usingBlock:block];

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:_observer];
}