How do I manually adjust the device orientation when closing the application deployment portlet?

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

I can detect the device orientation change to

[[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 to manually change the device orientation even when the App Deployment Info portrait is locked?

This one doesn't work

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

      

+3


source to share


4 answers


Turn on "Portrait" and "Landscape" in the project settings. Then use the method below to have the whole view manager disable autorotation -

- (BOOL)shouldAutorotate {
    return NO;
}

      

Then use below method for everyone 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 please use a category to disable autorotation.

Hope this helps you.

+4


source


Two steps:

  • Include terrain in deployment
  • In each controller view, viewDidLoad

    you need to include:

    //Swift
    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
    
    //Obj-C
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    
          



See also: How to programmatically set device orientation in iOS7?

+6


source


Instead of forcing the orientation change, you should subclass the navigation controller, which is terrain, and use that when presenting. Then your app will always be a portrait the way you want it. When using this navigation controller, it will be landscape.

#import "RotationAwareNavigationController.h"

@implementation RotationAwareNavigationController

-(BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

      

You should call it like below:

RotationAwareNavigationController *navController = [[RotationAwareNavigationController alloc] initWithRootViewController:aViewController];
[self presentViewController:navController animated:NO completion:nil];

      

+2


source


Updated answer for Swift 3+:

let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

      

0


source







All Articles