ios – presentViewController更改控制器的方向

我想从视图控制器播放视频.当我呈现它时,它呈现为纵向方向,因此视图转动.它只发生在iPhone上,而不是iPad上.

有一个ViewController> MyItemsController>视频控制器

当我关闭VideoController时,视频控制器的父控制器(MyItemsController)就像:

视图控制器的故事板是:

代码是:

-(void)playMoviesForItems:(NSArray *)shopItems{
    VideoPlayerViewController* moviePlayer = [self.storyboard instantiateViewControllerWithIdentifier:@"videoPlayerController"];
    moviePlayer.modalPresentationStyle = UIModalPresentationCurrentContext;
    moviePlayer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:moviePlayer animated:NO completion:nil];
}

我将代码移到app delegate中:

-(void)playMoviesForItems:(NSArray *)shopItems{
VideoPlayerViewController* mp = [[self getStoryboard] instantiateViewControllerWithIdentifier:@"videoPlayerController"];
[mp playMoviesForItems:shopItems];
[self pauseBackgroundMusic];

[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:mp animated:YES completion:NULL];
}

这一次,一切似乎都没问题.电影正在播放,我可以听到声音,但看不到视频.为什么?

最佳答案 虽然接受的答案被拒绝投票,因为它没有回答问题,这里有一些适用于iOS9:

呈现ViewController时(模态)调用的方法是preferredInterfaceOrientationForPresentation.此方法必须返回方向.
您应该检查演示者的方向并将其作为首选项返回:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
        UIInterfaceOrientation topOrientation = self.navigationController.visibleViewController.interfaceOrientation;
        UIInterfaceOrientation presentingOrientation = self.presentingViewController.interfaceOrientation;

        return   presentingOrientation ? presentingOrientation : topOrientation ? topOrientation : UIInterfaceOrientationLandscapeLeft;
}

topOrientation包含可见视图控制器的方向,而presentationOrientation是被调用到presentViewController的方向:动画…
一般来说,我建议你创建一个“基础”UIViewController类,并从中继承.这样,所有视图控制器都将受益于此代码.

点赞