5

In my project I have allowed only portrait rotation, but for one ViewController I would like to enable also landscape. I'm presenting this ViewController as ModalViewController, I've tried using methods - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation or iOS 6 methods like -(NSUInteger) supportedInterfaceOrientations but nothing actually worked. The view didn't rotate although those methods got called.

After this I've tried to rotate it by myslef with listening to those notifications :

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

but even though I was able to manually rotate the view in method didRotate: it's very messy and I can't rotate the StatusBar.

I would really like to use standard methods like shouldAutorotateToInterfaceOrientation, but I don't know how. Anyone?

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
animal_chin
  • 6,610
  • 9
  • 37
  • 41
  • What orientation is set for that perticular viewController in storyboard? – SangamAngre Dec 26 '12 at 12:40
  • if you mean the orientation setting in attributes inspector its set to inherited. I cant set just portrait or just landscape, i want both of theese to be supported. – animal_chin Dec 26 '12 at 12:49
  • What are the suppoerted orientations set for your project? – SangamAngre Dec 26 '12 at 12:56
  • well ive tried both setting them to all orientations (like in the answer from Junaid Sidhu) and both turning them just to portrait. If I turn them all on, i can rotate EVERY view controller in project (i dont wont this)and i dont know how to disable rotation on those other view controllers... – animal_chin Dec 26 '12 at 13:06
  • 1
    See This Thread Has same u looking for ...http://stackoverflow.com/questions/12577879/shouldautorotatetointerfaceorientation-is-not-working-in-ios-6/12581799#12581799 – Kamar Shad Dec 26 '12 at 13:08
  • In all other view controller if you want to disable the landscape view replace code of shouldAutorotateToInterfaceOrientation with code of Portrait view and the viewcontroller where you want all orientation just write return YES. – SangamAngre Dec 26 '12 at 13:09

4 Answers4

6

Add this in your app delegate.m

# pragma mark - Rotation

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if ([self.window.rootViewController isKindOfClass:[MVYSideMenuController class]]) {

        // Get topmost/visible view controller
        UIViewController *currentViewController = [self.window.rootViewController.childViewControllers lastObject];
        // Check whether it implements a dummy methods called canRotate
        if ([currentViewController respondsToSelector:@selector(canRotate)]) {
            // Unlock landscape view orientations for this view controller
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }

    // Only allow portrait (standard behaviour)
    return UIInterfaceOrientationMaskPortrait;
}
-(void)canRotate
{
}

and then add this method

-(void)canRotate
{
    // just define the method, no code required here
}

in every ViewController (.m files) where you want to provide rotation. You can also include here -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method to react when the device rotates:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation) {
        case 1:
        case 2:
            //NSLog(@"portrait");
            break;

        case 3:
        case 4:
            //NSLog(@"landscape");
            break;
        default:
            //NSLog(@"other");
            break;
    }
}
eduludi
  • 1,618
  • 21
  • 23
2

Subclass a navigation controller for your screen that requires rotation.

In the .m

// Older versions of iOS (deprecated) if supporting iOS < 5
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation    {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

// iOS6
 - (BOOL)shouldAutorotate {
return YES;
 }

 // iOS6
 - (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}

This overrides the rotation methods set in the summary page for iOS 6.

In iOS 6 the view controllers only look to there parent or root controller for rotation methods

JSA986
  • 5,870
  • 9
  • 45
  • 91
0

can't you just call the shouldAutoRotateToInterfaceOrientation in the viewDidLoad and viewWillAppear like so:

[self shouldAutorotateToInterfaceOrientation];

that should call the method if its in your ViewController

user2506891
  • 143
  • 2
  • 9
-1

enter image description here

Implement is in all controller and Return on that interfaceOrientation which you need for a specific controller

For All

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{

 return YES;
}

For Landscape

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

For Portrait

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
  • 1
    this was also my first guess...setting all orientations in project settings is OK and app rotates...but those methods never get called on iOS6 so you cant control it on every view controller separatelly...instead method supportedInterfaceOrientations is called just after viewDidLoad... But somehow even if I return UIInterfaceOrientationPortrait in it nothing changes and im able to rotate view controller to all orientations... – animal_chin Dec 26 '12 at 13:04
  • Same problem with me, i want to support all orientations for a single view, all the other views must support only portrait orientation only. App works fine in iOS5 but in iOS6 it rotates even when i am not supporting landscape orientations. Please help – iMemon Apr 12 '13 at 12:37