1
votes

In my view controller, I implement two methods for controlling interface orientation:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

In the supportedInterfaceOrientations method, I return UIInterfaceOrientationMaskPortrait but at that time I realized that the shouldAutorotate method is not being called.

But I change to return UIInterfaceOrientationPortrait in the supportedInterfaceOrientations method. The shouldAutorotate method is being called, but there is an error that mentions in following:

UIApplicationInvalidInterfaceOrientation, reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'

By the way, I select all orientations in the supported interface orientations.

EDITED

i use viewController and embed with navigationController. here is AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate,UINavigationControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) UINavigationController *navController;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end

in didFinishLaunchingWithOptions method under AppDelegate.m

      navController = (UINavigationController *)self.window.rootViewController;
                IPad_HomeViewController *rootVC=(IPad_HomeViewController *)navController.topViewController;
                rootVC.managedObjectContext = self.managedObjectContext;
return YES;

in my IPad_HomeViewController,

@interface IPad_HomeViewController : UIViewController <UINavigationControllerDelegate>
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end
5
I don't think code is need just to change its orientation, you do this in the menu Targets->Summary-> Supported Device Orientations and change as following. - Programmer...
@Programmer... You have to do it in code if a particular screen supports different orientations from the broader app. - Rob
check my answer for ios 6 and also pre ios 6 sample codes. hope it helps. - Adrian P
Your app delegate looks fine. Try setting autorotate to yes in all of the view controllers and then set the rotation desired. See if that works. I actually built a demo single view project and tested the code. It works fine. - Adrian P
I return Yes in shouldAutorotate method and set UIInterfaceOrientationMaskPortrait in supportedInterfaceOrientations method. but shouldAutorotate is not being called and error still remains. btw i also have shouldAutorotateToInterfaceOrientation method for ios5. if possible, can you send ur sample code to me? Thanks really. - Dartfrog Imi

5 Answers

2
votes
- (BOOL) shouldAutorotate {
    return YES;
}

// for landscape
- (NSInteger) supportedInterfaceOrientations {
    return (1 << UIInterfaceOrientationLandscapeLeft) | 
           (1 << UIInterfaceOrientationLandscapeRight);
}

As noted above, check out the mask values for specific orientations : UIInterfaceOrientationMask values.UIInterfaceOrientationMask

1
votes

Two points:

  1. Your error message makes complete sense because it makes no sense to use UIInterfaceOrientationPortrait as a return value from supportedInterfaceOrientations, which is returning a bit mask. Use one of the UIInterfaceOrientationMask values.

  2. You seem to be concerned that if you use the proper UIInterfaceOrientationMaskPortrait, that iOS doesn't appear to call shouldAutorotate. It may only call shouldAutorotate if, having considered the physical device's physical orientation and the app's current orientation against the supportedInterfaceOrientations that a rotation might be needed. Why should it check shouldAutorotate if it concludes that the device is in an acceptable orientation already?

See supportedInterfaceOrientations and Handling View Rotations for more information.

1
votes

I know this sounds pretty elementary, but I was wracking my brain to figure out orientation issues while testing on my iPhone - I had the physical auto lock mode in Portrait mode - so nothing I changed programmatically mattered - thought this should be troubleshooting step number 1!

0
votes

In order to change the orientation setting, select menu Targets->Summary-> Supported Device Orientations and change as following.

If the buttons for the orientation are dark then that means you have selected it as one of the orientation. enter image description here

0
votes

rather than using an integer to declare the orientation why don't you use a bool and plug in a if statement in there to detect whatever orientation you want. here is a example code that i hope would help you:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
    return YES;
}
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    return YES;
}
return NO;
}

you can add all of the orientations in the if statement and it should work just fine. adrian

Edit:

and if you want to have an option for ios 6 the below code should work just fine for you.

- (BOOL) shouldAutorotate
{
return YES;
 }

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft   | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

you can change just about all the supported orientations with this in ios 6. happy coding

EDIT 1:

to rotate only certain viewcontrollers in a certain way, just use the ios 6 code that i posted above in all viewcontrollers. here are the steps:

  1. in the project level where all four internfaceorientations are located, go ahean and turn everything off so app would go to default.

  2. implement the ios 6 code that i supplied in all viewcontrollers.

  3. rather than yes declare no in shouldautorotate method.
  4. in the second method, plug in any type of orientation you want.

this should do the trick for you. happy coding