UIViewController的模态表示和旋转

时间:2015-06-18 11:36:26

标签: ios objective-c rotation autolayout presentmodalviewcontroller

所以我的问题看起来像这样:

这是演示视图控制器的方法:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
       || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) && self.interfaceShouldRotate) {
        if(self.videoFullscreen == nil) {
            self.videoFullscreen = [self.storyboard instantiateViewControllerWithIdentifier:videoFullscreenController];
            self.videoFullscreen.delegate = self;
        }
        [self.videoFullscreen setVideoView:self.matchVideoView.containerViewForVideo];
        [UIApplication sharedApplication].statusBarHidden = YES;
        [self presentViewController:self.videoFullscreen animated:YES completion:nil];
    }
}

我的想法是通过呈现UIViewController以横向模式呈现视频播放器,并使用视频播放器视图在此控制器中设置视图:

- (void)setVideoView:(UIView *)videoView {
NSParameterAssert(videoView);

_videoView = videoView;
[self.view addSubview:_videoView];
_videoView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = @{@"video":_videoView};

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[video]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[video]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views]];

}

这是委托方法,通知根视图控制器它将被解雇,并且视频播放器应该以纵向方式再次嵌入。

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait
       || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self.delegate fullscreenViewControllerdidRotateToPortrait:self];
    }
}

委托方法执行此操作:

- (void) removeViewAndDismissFullscreenVideoController {
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
    self.matchVideoView.containerViewForVideo = self.videoFullscreen.videoView;
    [UIApplication sharedApplication].statusBarHidden = NO;
}

- (NSUInteger)supportedInterfaceOrientations- (BOOL)shouldAutorotate在自定义UINavigationController中设置。全部应用仅处于纵向模式。播放视频时,通知切换支持UIInterfaceOrientationMaskAll的方向,用户可以使用嵌入的视频旋转屏幕。当设备旋转为横向时,视频播放器将移至全屏。

那么问题出在哪里?在iOS 8上它运行良好,但在iOS 7中,当视频播放器控制器被解除时,带有嵌入式播放器的控制器保持横向模式,但只能查看,因为- (void)viewDidAppear:(BOOL)animated当我检查self.interfaceOrientation UIViewController时处于纵向模式(视图为横向模式)。一切都与Autolayout有关。 知道如何找到问题原因并解决问题吗?

修改

以下答案。它必须位于View Controller中,在子视图中嵌入了视频播放器,当在横向中VideoView被移动到呈现的View Controller视图时。此外,这也不起作用。

解决方案:

我已经移动了纵向旋转代码,将全屏视频解除为didRotate(仅适用于iOS7)并进行自定义转换。那解决了问题。

1 个答案:

答案 0 :(得分:1)

我认为不需要在该类中管理它只需在AppDelegate方法中编写一些代码来仅旋转单个视图。如下所示

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone )
    {
        if ([[self.window.rootViewController presentedViewController]
             isKindOfClass:[MPMoviePlayerViewController class]]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }

        return UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskAll;
}

该代码告诉“MPMoviePlayerViewController”显示屏幕旋转设置为启用,否则仅设置为纵向模式。

我希望它会对你有所帮助。