Allow video on landscape with only-portrait app

前端 未结 8 1394
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 06:35

I have a UIWebView included in a UIViewController which is a descendant of UINavigationController. It looks like this:

8条回答
  •  北海茫月
    2020-12-13 07:15

    I temporarily solved (through a hack) with the following code in the viewDidLoad of my controller. I have to specify that the code is specifically made for my case: since I explicitly disallow landscape orientation of my UINavigationController (see code above), the usual notification “UIDeviceOrientationDidChange” is not called when the playback finished and the window goes back to portrait. However, I hope there is a better option and this is a bug of the SDK, since it does not appear on iOS 7 and given the amount of auto-layout errors I get related to the video player (on which I have no control).

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // […]
    
         /* 
         Hack to fix navigation bar position/height on iOS 8 after closing fullscreen video
    
         Observe for “UIWindowDidRotateNotification” since “UIDeviceOrientationDidChangeNotification” is not called in the present conditions
         Check if the notification key (“UIWindowOldOrientationUserInfoKey”) in userInfo is either 3 or 4, which means the old orientation was landscape
         If so, correct the frame of the navigation bar to the proper size.
    
         */
        [[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
            if ([note.userInfo[@"UIWindowOldOrientationUserInfoKey"] intValue] >= 3) {
                self.navigationController.navigationBar.frame = (CGRect){0, 0, self.view.frame.size.width, 64};
            }
        }];
    }
    

    And then…

    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"UIWindowDidRotateNotification"];
    }
    

提交回复
热议问题