Supporting different orientations in iOS 6.0 for different view controllers
Asked Answered
C

2

6

I am having custom split view controller in my App with a master controller and a detailed controller.

- (id)initWithMasterController:(UIViewController*)aMasterController
            detailedController:(UIViewController*)aDetailedController;

The controllers provided for the master controller and details controller are UINavigationController.

As part of my app, there are two possible cases for orientation handling:

  1. When six combination of controllers are used in master and details controller, all the orientations are supported for the app.
  2. When there is a StudentDetailsViewController at the details controller alone, only two possible orientations can be supported. (Landscape)

When ever the device's orientation is changed, the below things happen in versions below iOS 6.0

  1. The -shouldAutorotateToInterfaceOrientation: method gets called. The implementation of that method is below: At run time, I forward the request to master controller and details controller with the same call.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {   
        BOOL res = [masterController shouldAutorotateToInterfaceOrientation:interfaceOrientation]
                   && [detailedController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        return res;
    }
    
  2. The masterController's -shouldAutorotateToInterfaceOrientation will return TRUE. The implementation of the method in StudentViewController is below.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (IS_IPAD) ? UIInterfaceOrientationIsLandscape(interfaceOrientation)
                         : UIInterfaceOrientationIsPortrait(interfaceOrientation);
    }
    

The ability to get information on the new orientation to be changed helps me to decide if rotation should be enabled or not.

With iOS 6.0:

When ever the device's orientation is changed, the below things happen in versions of iOS 6.0

  1. The method -shouldAutorotate of the split view controller gets called. Its implementation is below

    - (BOOL)shouldAutorotate {
        BOOL res = [masterController shouldAutorotate]
                   && [detailedController shouldAutorotate];
        return res;
     }
    
  2. The detailedController's shouldAutorotate calls the navigationController. The implementation of autorotate feature in StudentsController:

    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        return (UIInterfaceOrientationMaskLandscapeLeft
                | UIInterfaceOrientationMaskLandscapeRight);
    }
    

But with iOS 6.0, I am unable to control the orientation. Even though the supportedInterfaceOrientations method gets called, when the shouldAutorotate method of the StudentsDetailsController gets called, from the detailsController's shouldAutorotatemethod, the shouldAutorotateMethod does not obey the options mentioned in the supportedInterfaceOrientations method.

UPDATE:

I read the docs and the below notes are provided in the document.

sometimes you may want to dynamically disable automatic rotation. For example, you might do this when you want to suppress rotation completely for a short period of time. You must temporarily disable orientation changes you want to manually control the position of the status bar (such as when you call the setStatusBarOrientation:animated: method).

If you want to temporarily disable automatic rotation, avoid manipulating the orientation masks to do this. Instead, override the shouldAutorotate method on the topmost view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.

Is it possible to temporarily disable automatic rotation based on the current orientation?

Cementation answered 17/10, 2012 at 6:44 Comment(3)
The code formatting could not be done correctly..Cementation
Have you solved the issue? I have the same problem and can't figure out how to handle this both for ios5 and 6Noctambulism
No I have not solved it..Cementation
J
0

I believe this is some type of issue in iOS where the rootViewController does not consult the childViewController for their preferred orientation. However, you should try something like the following :

 if (self.interfaceOrientation != UIInterfaceOrientationPortrait) 
{
    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
}

to change the orientation back to portrait for a given view.

Jadejaded answered 31/12, 2012 at 1:43 Comment(0)
L
0

In your application delegate class define the following method, this method gets called before any other rotation methods in application.

Create a flag(isRotationEnabled) which will decide orientation of your app.

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

return self.isRotationEnabled ?
    UIInterfaceOrientationMaskAll :
    UIInterfaceOrientationMaskPortrait;
}

Change this flag based on different conditions in you app using the following code

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isRotationEnabled = NO;
Lathrop answered 10/5, 2013 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.