16

I have a MainViewController which has a button which pushes a new view (InfoViewController), via flip horizontailly. like so:

controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];

The MainView Controller supports Portrait and PortraitUpsideDown. Like so:

- (BOOL)shouldAutorotate {
    return YES;    
}

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

In my InfoViewController it also states the above code. In my AppDelegate it has this in the LaunchOptions:

[self.window setRootViewController:self.mainViewController];

In my app.plist file it supports all orientations. This is because other views need to support landscape as well. So On my MainViewController and InfoViewController I need only Portrait and PortraitUpsideDown. But on another view I need all orintations.

My MainViewController works fine, but my InfoViewController is working for all orientations.

I am having extreme diffulty trying to get this to work in iOS6. I have researched other posts and tried the assistance other people have provided, but had no luck whatsoever. Please can someone help me acheive this thank you. And I'm a Objective-C newbie :p

Mundi
  • 79,884
  • 17
  • 117
  • 140
ryryan
  • 3,890
  • 13
  • 43
  • 72
  • In the title it says "no rotation". In your penultimate paragraph it says it is working for all directions. Which one is it? – Mundi Sep 23 '12 at 18:42

4 Answers4

26

Don´t support all orientations in your app plist file, only those that your root view controller supports.

Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow: and shouldAutorotate methods:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}

Modal ViewControllers no longer get rotation calls in iOS 6: The willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are no longer called on any view controller that makes a full-screen presentation over itself—for example those that are called with: presentViewController:animated:completion:.

You can let the view controller that presents your modal view controller inform it of rotation. Also, now you use: presentViewController:animated:completion: to present the view controller. presentModalViewController:animated: is deprecated which you use in the code.

Sverrisson
  • 17,970
  • 5
  • 66
  • 62
  • Right, ok. I have removed all the orientations other than Portrait (home button bottom) from the plist. But now I have a view which requires all orientations. I have put supportedInterfaceOrientations and then listed all, but it doesn't work. Thanks. – ryryan Sep 23 '12 at 19:01
  • If you don´t have the shouldAutorotate returning YES, then your supportedInterfaceOrientations is never called and the view doesn´t rotate. What specifically do you mean by doesn´t work? – Sverrisson Sep 23 '12 at 19:05
  • The shouldAutorotate for that view is returning YES. – ryryan Sep 23 '12 at 19:07
  • Then you return the supportedInterfaceOrientations that you want to allow rotation to. – Sverrisson Sep 23 '12 at 19:09
  • Now that I have changed the plist, the infoViewController works fine (doesn't rotate = good). However, the FlipsideViewController doesn't rotate (bad), I want that to rotate. In the FlipsideViewController.m I have returned YES for shouldAutorotate and have also returned all for supportedInterfaceOrientations. But it still doesn't rotate. – ryryan Sep 23 '12 at 19:17
  • 7
    Modal view controllers no longer get rotation calls in iOS 6 – Sverrisson Sep 23 '12 at 19:20
  • So, it looks like I'm f****d then. – ryryan Sep 23 '12 at 19:35
  • You can let the view controller that presents your modal view controller allow all rotations when the modal view is being presented. – Sverrisson Sep 23 '12 at 19:39
  • You put an if statement in your supportedInterfaceOrientations method that checks if you are presenting your modal view and returns the correct mask. Please accept the answer and start a discussion if you need further assistance. – Sverrisson Sep 23 '12 at 19:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17035/discussion-between-hart1994-and-hannes-sverrisson) – ryryan Sep 23 '12 at 19:48
3

I have solved similar problems, while using tab bar controller.

Subclass UITabBarController. Implement these methods:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


- (BOOL)shouldAutorotate
{
    NSLog(@"Orientation:%d", [[UIDevice currentDevice] orientation]);

    for (UIViewController *viewController in self.viewControllers) {
        [viewController shouldAutorotate];
    }

    return YES;
}

If you want to handle rotations in controllers inside tabbarcontroller, in each of the controllers in the tab bar controller implement those methods too and write code to handle orientation change. If you don't want to handle it, then you don't need to implement those methods. TabBarControllers methods will always run when orientation changes. Even twice for unknown reason.

Yes, and don't forget to delete all shouldAutorotate methods. I moved to the new orientation model completely. If you want to make them remain, probably, it will be harder.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
0

Make a category by subclassing UINavigationController and implement following methodes in .h file

-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;



 in .m file

-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
 {
return [self.topViewController supportedInterfaceOrientations];
 }

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return [self.topViewController preferredInterfaceOrientationForPresentation];
 } 

and implement following methodes in the view controller class ,class u want to enable rotation

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


- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return UIInterfaceOrientationLandscapeLeft;
}
Sishu
  • 1,510
  • 1
  • 21
  • 48
0

add this code on Subclass UITabBarController .m

@implementation UINavigationController (rotation)
//temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers.
- (NSUInteger)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}
@end

@implementation NameClassUITabBar

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

@end

Here I've posted my solution/experince in tab bar controller with rotations: http://luterr.blogspot.sg/2015/04/example-code-uiinterfaceorientationmask.html