4

我想从视图控制器播放视频。当我展示它时,它就像纵向展示一样,所以视图会转动。它只发生在 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];
}

我将代码移到应用程序委托中:

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

这一次,一切似乎都很好。电影正在播放,我能听到声音,但看不到视频。为什么? 在此处输入图像描述

4

6 回答 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 类,并从它继承。这样,您的所有视图控制器都将从该代码中受益。

于 2015-11-22T15:53:26.407 回答
1

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

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

希望这可以帮助!

于 2014-08-18T04:02:27.817 回答
1

斯威夫特 4.1 版本

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

override var shouldAutorotate: Bool {
    return false
}

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

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

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

于 2018-09-04T11:22:08.700 回答
0

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

- (UIInterfaceOrientationMask) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
于 2013-06-01T07:30:55.463 回答
0
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationMaskLandscape);
}

-(BOOL)shouldAutorotate
{
    return YES;
}

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

尝试这个...

于 2013-06-01T07:35:29.260 回答
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 类],command+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 设置为横向模式,最后但并非最不重要的一点是,确保您的项目部署信息选择您想要的方向。

于 2014-04-14T08:43:21.520 回答