Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parent View Controller doesn't rotate while Child View Controller is presented

I have a parent view controller that supports all four orientations. It presents a child view controller, and if the device is rotated while the child view controller is presented, and then you dismiss the child, the parent view controller is not rotated correctly. Here is the code I use for rotation in the parent view controller:

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

if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

    newestIssueCoverImageButton.frame = CGRectMake(40, 20, 688, 865);
    shadow.frame = CGRectMake(40, 20, 688, 865);
    recordView.frame = CGRectMake(0, 44, 768, 916);
    classifiedsWebView.frame = CGRectMake(0, 44, 768, 916);

} else {

    newestIssueCoverImageButton.frame = CGRectMake(269, 20, 486, 613);
    shadow.frame = CGRectMake(269, 20, 486, 613);
    recordView.frame = CGRectMake(0, 44, 1024, 660);
    classifiedsWebView.frame = CGRectMake(0, 44, 1024, 660);

}

The parent looks at though this code wasn't used and I just rotated the device. How do I call this code and ensure that the parent view controller is rotated correctly when the user is currently looking at the child? Thanks for your help.

like image 600
Jack Humphries Avatar asked Dec 21 '22 01:12

Jack Humphries


1 Answers

From Apple's docs:

If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, the viewWillLayoutSubviews method is called when the view becomes visible. Your implementation of this method can call the statusBarOrientation method to determine the device orientation.

I added a BOOL to track when it is not the top viewController, then:

- (void)viewWillLayoutSubviews {

    if (notTopController) {

        [self willRotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] duration:0];
        [self willAnimateRotationToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] duration:0];

       notTopController = NO;

}

Works good for me so far.

like image 115
Phil Turner Avatar answered Apr 27 '23 01:04

Phil Turner