简体   繁体   中英

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:

- (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.

- (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. When video is playing, notification switch supported orientation to UIInterfaceOrientationMaskAll and user can rotate screen with embeded Video. 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). 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. 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. 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. 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.

I hope it will helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM