允许使用仅限纵向应用的横向视频 [英] Allow video on landscape with only-portrait app

查看:13
本文介绍了允许使用仅限纵向应用的横向视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

The app is portrait only. When I play the video I want the user to be able to rotate the device and see the video in landscape mode. I use this code to allow it:

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

然后在我的 UINavigationController 中(所以当视频结束时,视图不会以横向呈现,而只会以纵向呈现):

And then in my UINavigationController (so when the video finishes the view is not presented in landscape but only in portrait):

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

一切正常:

但随后视频播放完毕(或用户点击完成")并且屏幕返回到底层视图,这就是发生的情况:

But then the video is done playing (or the user taps ‘Done’) and the screens return to the underlying view, this is what happens:

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

As you can see, the navigation bar slips under the status bar. Additionally, I get a lot of auto-layout errors in the logs: http://pastebin.com/09xHzmgJ

知道如何解决这个问题吗?

Any idea about how to solve this?

推荐答案

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

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

然后……

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

这篇关于允许使用仅限纵向应用的横向视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆