简体   繁体   English

UIViewController的模态表示和旋转

[英]Modal presentation of UIViewController and rotation

So my problem looks like this: 所以我的问题看起来像这样:

This is method for presentation view controller: 这是表示视图控制器的方法:

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

My idea is to present Video Player in landscape mode by presenting UIViewController and set view in this controller with Video Player view: 我的想法是通过呈现UIViewController并以Video Player视图在此控制器中设置视图,以横向模式呈现Video Player:

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

} }

This is delegate method that informs root view controller that it will dismiss and Video Player should be embeded again in portrait orientation. 这是一个委托方法,它通知根视图控制器将其关闭,并且应将Video Player重新以纵向方向嵌入。

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

Delegate method does this: 委托方法执行此操作:

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

- (NSUInteger)supportedInterfaceOrientations and - (BOOL)shouldAutorotate are set in custom UINavigationController .Whole app is in portrait mode only. - (NSUInteger)supportedInterfaceOrientations InterfaceOrientations和- (BOOL)shouldAutorotate在自定义UINavigationController中设置。整个应用程序仅处于纵向模式。 When video is playing, notification switch supported orientation to UIInterfaceOrientationMaskAll and user can rotate screen with embeded Video. 播放视频时,通知将支持的方向切换为UIInterfaceOrientationMaskAll并且用户可以旋转嵌入视频的屏幕。 When device will rotate to landscape, video player moves to full screen. 当设备旋转到横向时,视频播放器将移至全屏。

So where is problem? 那么问题出在哪里呢? On iOS 8 it works good, but in iOS 7 when video player controller is dismissed, controller with embeded player stays in landscape mode, but only view because in - (void)viewDidAppear:(BOOL)animated when I check self.interfaceOrientation UIViewController is in portrait mode (view is landscape mode). 在iOS 8上它运行良好,但是在iOS 7中,当视频播放器控制器被关闭时,具有嵌入式播放器的控制器将保持横向模式,但只能查看,因为当我检查self.interfaceOrientation UIViewController- (void)viewDidAppear:(BOOL)animated时是- (void)viewDidAppear:(BOOL)animated在纵向模式下(视图为横向模式)。 Everything is with Autolayout. 一切都与自动排版有关。 Any idea how to find cause of problem and fix it? 任何想法如何找到问题的原因并解决它?

edit: 编辑:

To the answer below. 到下面的答案。 It must be in View Controller that has embeded Video player in subview, when in landscape VideoView is moved to presented View Controller view. 当横向移动VideoView到显示的View Controller视图时,它必须在将Video Player嵌入子视图的View Controller中。 Besides, this doesn't work also. 此外,这也不起作用。

Solution: 解:

I've moved code for portrait rotation, that dismiss fullscreen to didRotate (only for iOS7) and make custom transition. 我已经移动了用于肖像旋转的代码,将全屏显示为didRotate(仅适用于iOS7)并进行了自定义过渡。 That solve problem. 那解决了问题。

I think no need for manage it in that class just write some code in AppDelegate methods for rotate single view only. 我认为无需在该类中对其进行管理,只需在AppDelegate方法中编写一些代码即可(仅用于旋转单个视图)。 as like follows 如下

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

that code tells "MPMoviePlayerViewController" is showing rotation of screen set as enable otherwise set as portrait mode only. 该代码告诉“ MPMoviePlayerViewController”正在显示屏幕旋转为启用状态,否则仅设置为纵向模式。

I hope it will helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有自定义过渡的UIViewController; 模态呈现后丢失 - UIViewController with Custom Transition; Lost After Modal Presentation UIViewController在模态表示后更改维度 - UIViewController changes dimension after modal presentation iOS 13 UIViewController 模态表示阴影 - iOS 13 UIViewController modal presentation shadow UIViewController不知道旋转与显示模式视图 - UIViewController isn't aware of rotation with a modal view being shown 我如何锁定作为模态表单表示的UIViewController的旋转? - How I lock rotation on a UIViewController presented as modal form sheet? 缓存UIViewController进行演示/推送 - Cache UIViewController for presentation/pushing UIViewController的循环展示 - Circular presentation of UIViewController 具有演示样式formSheet的模态UIViewController无法在iPhone XS Max和iPhone XR上正确显示 - Modal UIViewController with presentation style formSheet is not displayed correctly on iPhone XS Max and iPhone XR UIViewController 全屏演示之上自动演示 - UIViewController automatic presentation on top of full screen presentation 旋转自定义UIViewController过渡 - Custom UIViewController transition with rotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM