自动旋转在UIWebView中从HTML5 <video>播放的视频

Jil*_*ouc 5 iphone uiviewcontroller autorotate html5-video

我的应用程序出于某些原因仅支持纵向方向.
但是,在某些情况下,我需要显示来自UIWebView(带video标签)的视频,如果用户可以纵向或横向查看它,那将会很不错.

控制器配置如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
Run Code Online (Sandbox Code Playgroud)

结果:视频仅以纵向模式播放(确定,非常期待).

我尝试过:
- 设置此项以在用户启动视频
时支持所有方向- 当视频停止时,返回"仅限肖像"

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    // autorotationEnabled is toggled when a video is played / stopped
    return autorotationEnabled ? YES : UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
Run Code Online (Sandbox Code Playgroud)

结果:横向模式可用(太棒了!)但是如果用户在横向播放时点击"完成",则一旦玩家被解雇(不太好),前一个视图将以横向模式显示.

当玩家被解雇时,有没有人知道如何防止控制器以横向模式显示?
(使用UIDevice的私有方法setInterfaceOrientation不是一个选项)

小智 2

我也做过类似的事情。您必须修改 UIView 堆栈,以强制应用程序在弹出控制器时调用 shouldAutorotateToInterfaceOrientation。

在您的 WebViewController 中设置为允许自动旋转:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
Run Code Online (Sandbox Code Playgroud)

在父控制器中不允许自动旋转:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)

创建并分配 UINavigationControllerDelegate。
在委托中,当控制器弹出或推送时临时修改 UIView 堆栈:

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

// Force portrait by changing the view stack to force autorotation!
if ([UIDevice currentDevice].orientation != UIInterfaceOrientationPortrait) {
    if (![viewController isKindOfClass:[MovieWebViewController class]]) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UIView *view = [window.subviews objectAtIndex:0];
        [view removeFromSuperview];
        [window insertSubview:view atIndex:0];
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一种肮脏的黑客行为,但它确实有效。