presentViewController更改控制器的方向

问题描述 投票:4回答:6

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

有一个ViewController> MyItemsController> VideoController

当我关闭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];
}

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

ios screen-orientation
6个回答
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
投票

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

发生在我身上的是,我试图用另一个不在等级中的ViewController呈现新的ViewController,它只是将它的视图添加为层次结构中第三个subviewViewController。为了解决这个问题,我刚刚做了这个最后的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];

应该做什么的例子:

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

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

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

希望这可以帮助!


0
投票

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

- (UIInterfaceOrientationMask) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

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

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支持横向模式,最后但并非最不重要,确保您的项目部署信息选择您想要的方向。


0
投票

Swift 4.1版本

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

override var shouldAutorotate: Bool {
    return false
}

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

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

您需要提供视图控制器才能使其工作(它对推送的视图控制器不起作用)


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

-(BOOL)shouldAutorotate
{
    return YES;
}

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

试试这个...

© www.soinside.com 2019 - 2024. All rights reserved.