presentViewController更改控制器的方向

时间:2013-06-01 07:19:20

标签: ios screen-orientation

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

有一个ViewController> MyItemsController> VideoController

enter image description here

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

enter image description here

视图控制器的故事板是:

enter image description here

代码是:

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

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

6 个答案:

答案 0 :(得分:2)

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

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

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

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

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

答案 1 :(得分:1)

几分钟前我遇到了同样的问题。

我发生的事情是,我试图将新的ViewController与另一个ViewController一起展示在层次结构中,它只是将其视图添加为层次结构中的subview个第三个ViewController。为了解决这个问题,我刚刚提出了这个新问题。{/ p>

不做的示例:

ViewController

应该做什么的例子:

UIViewController *secondController = [UIViewController alloc] init];
[rootViewController presentViewController:secondController animated:NO completion:nil];

UIViewController *controllerOutsideHierarchy = [UIViewController alloc] init];
[secondController.view addSubview:controllerOutsideHierarchy.view];

[controllerOutsideHierarchy presentViewController:thirdController animated:NO completion:nil];

希望这有帮助!

答案 2 :(得分:0)

确保在视图控制器中添加这两个:

- (UIInterfaceOrientationMask) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

答案 3 :(得分:0)

您必须在self.window.rootViewController'类中添加以下方法:

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}


@end

如果你的rootViewController是[UINavigationController类],命令+ n添加一个UINavigationController类,就像我下面的代码一样

@implementation UINavigationController (Rotation_For_iOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}


@end

然后,转到viewController.m,你想成为横向或纵向模式,也许因为你是你的VideoController !!!!!! 添加以下方法:

#pragma mark
#pragma mark ----- Orientation Control For iOS 6/7 -----

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight||toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

现在我将我的vc设置为支持横向模式,最后但同样重要的是,确保您的项目部署信息选择您想要的方向。

答案 4 :(得分:0)

Swift 4.1版本

要防止自动旋转方向,您需要覆盖shouldAutorotate变量

override var shouldAutorotate: Bool {
    return false
}

要以横向模式显示视图控制器,您需要覆盖preferredInterfaceOrientationForPresentation变量

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeLeft// or .landscapeRight
}

您需要提供视图控制器以使其正常运行(对于推送视图控制器不起作用)

答案 5 :(得分:-1)

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationMaskLandscape);
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskLandscape);
}

试试这个......