0

I have tried many codes from this website and others too.... But nothing seems to be working for me. Here is my code...

- (IBAction)orientation:(id)sender {
    self.currentDeviceOrientation = [[UIDevice currentDevice] orientation];
    int cur = self.currentDeviceOrientation;
    int port = UIInterfaceOrientationPortrait;
    int landl = UIInterfaceOrientationLandscapeLeft;
    int landr = UIInterfaceOrientationLandscapeRight;

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        NSNumber *value= [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"orientation"];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:YES];
    }
}

on the click of a button, I want the screen to change from its current orientation i.e, if the screen is in portrait mode, it should change in to landscape and vice-verse.

Idan
  • 5,405
  • 7
  • 35
  • 52
  • Possible duplicate of [How do I programmatically set device orientation in iOS7?](http://stackoverflow.com/questions/20987249/how-do-i-programmatically-set-device-orientation-in-ios7) – Mike Taverne May 09 '17 at 06:42

2 Answers2

0

You need to set orientation as per your needs.

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Try this and change as what you need.

- (IBAction)orientation:(id)sender {

        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){

        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        NSLog(@"landscape left");

        }

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){

        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        NSLog(@"landscape right");

        }

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){

        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        NSLog(@"portrait");

        }


}
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19
  • This works, but the problem is, this should not be in if case, because this does not wait for the second button click, i.e, on one click its keeps changing the orientations..... So my problem got solved when i have kept them in switch case....... – Ruthvik Reddy May 09 '17 at 09:29