仅使用纵向应用在景观上允许视频

ent*_*pid 32 objective-c uiwebview uinavigationcontroller screen-rotation ios

我有一个UIViewController中包含的UIWebView,它是UINavigationController的后代.它看起来像这样:

主要观点

该应用程序仅限肖像.当我播放视频时,我希望用户能够旋转设备并以横向模式观看视频.我使用这段代码来允许它:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    id presentedViewController = [self topMostController];
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

    if ([className isEqualToString:@"MPInlineVideoFullscreenViewController"] ||
        [className isEqualToString:@"MPMoviePlayerViewController"] ||
        [className isEqualToString:@"AVFullScreenViewController"]) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIViewController *)topMostController {
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}
Run Code Online (Sandbox Code Playgroud)

然后在我的UINavigationController中(所以当视频完成后,视图不会以横向呈现,只能以纵向呈现):

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
Run Code Online (Sandbox Code Playgroud)

一切都很完美:

视频肖像 视频风景

但是视频完成播放(或者用户点击'完成')并且屏幕返回到底层视图,这是发生的事情:

导航栏问题

如您所见,导航栏在状态栏下滑动.另外,我在日志中遇到了很多自动布局错误:http://pastebin.com/09xHzmgJ

有关如何解决这个问题的任何想法?

ent*_*pid 17

我通过控制器中的以下代码暂时解决(通过黑客攻击)viewDidLoad.我必须指定代码专门针对我的情况:因为我明确禁止我的UINavigationController的横向方向(参见上面的代码),当播放完成并且窗口返回到肖像时,通常不会调用通知"UIDeviceOrientationDidChange".但是,我希望有一个更好的选择,这是SDK的一个错误,因为它没有出现在iOS 7上并且给出了与视频播放器相关的自动布局错误(我无法控制) .

- (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};
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

然后…

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"UIWindowDidRotateNotification"];
}
Run Code Online (Sandbox Code Playgroud)


Sta*_*ich 5

我遇到了完全相同的问题,并使用了与@entropid相同的代码.然而,接受的解决方案对我不起作用.

花了我几个小时的时间来提出以下一线修复,让事情对我有用:

- (void)viewWillLayoutSubviews {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,这应该是公认的解决方案.确认工作正常. (3认同)