How to manually set the device orientation when App Deployment Info portrait is locked?

user3745996 picture user3745996 · May 21, 2015 · Viewed 10.8k times · Source

I dont want my app to be landscaped and always be porttrait.So i made my app Deployment Info to set portrait only. but when i need to display any image or videos in my app,i need landscape mode for better display.

I can detect the device orientation change by

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
    case UIDeviceOrientationPortrait:
        /*  */
        break;

    case UIDeviceOrientationPortraitUpsideDown:
        /*  */
        break;
    case UIDeviceOrientationLandscapeLeft:
       /*  */
       break;

    case UIDeviceOrientationLandscapeRight:
       /*  */
       break;
    default:
        break;
};
}

But how do i manually change my device orientation even when App Deployment Info portrait is locked ?

This doesnt worked

NSNumber *value=[NSNumber numberWithInt: UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

Answer

Santu C picture Santu C · May 21, 2015

Please enable Portrait & Landscape from your project setting. Then use below method for all viewcontroller to autorotation turn off -

- (BOOL)shouldAutorotate {
    return NO;
}

Then use below method to all except LandScapeViewControler -

- (void)viewWillAppear:(BOOL)animated {

    [[UIDevice currentDevice] setValue:
     [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                forKey:@"orientation"];
}

Use below method for LandScapeViewControler -

- (void)viewWillAppear:(BOOL)animated {

        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
                                    forKey:@"orientation"];
    }

If you are using NavigationController and TabBarController then please use category to turn off autorotation .

Hope this will be help you.