1

So I wam trying to forcefully change the device orientation at a part of my code by doing the following:

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
        [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown){
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    }

however the orientation didn't change. Why is this? Basically the current orientation is portrait and I want it to be forced to be in landscape

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
adit
  • 32,574
  • 72
  • 229
  • 373
  • It is working because you have to use `attemptRotationToDeviceOrientation` to device orientation. – CodaFi May 20 '12 at 04:57
  • you mean it's not working? also with attemptRotationToDeviceOrientation I can't force it to a particular orientation (i.e: supply the orientation I want) – adit May 20 '12 at 05:01

4 Answers4

5

If Inder Kumar Rathore's suggestion works, that's great. But the documentation describes the orientation property as being read-only, so if it works, I'm not sure you can rely on it working in the future (unless Apple does the smart thing and changes this; forcing orientation changes, regardless of how the user is currently holding their device, is such an obvious functional need).

As an alternative, the following code inserted in viewDidLoad will successfully (and somewhat curiously) force orientation (assuming you've already modified you shouldAutorotateToInterfaceOrientation as roronoa zorro recommended):

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}

Clearly, this does it if the user is currently holding their device in portrait orientation (and thus presumably your shouldAutorotateToInterfaceOrientation is set up for landscape only and this routine will shift it to landscape if the user's holding their device in portrait mode). You'd simply swap the UIDeviceOrientationIsPortrait with UIDeviceOrientationIsLandscape if your shouldAutorotateToInterfaceOirentation is set up for portrait only.

For some reason, removing the view from the main window and then re-adding it forces it to query shouldAutorotateToInterfaceOrientation and set the orientation correctly. Given that this isn't an Apple approved approach, maybe one should refrain from using it, but it works for me. Your mileage may vary. But this SO discussion also refers to other techniques, too.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
4

Works perfect on iOS7

If You want Potrait then:

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

if you want Landscape:

[[UIDevice currentDevice] setValue:
                      [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight]
                            forKey:@"orientation"];
Mike Akers
  • 12,039
  • 14
  • 58
  • 71
Arpan Dixit
  • 995
  • 1
  • 12
  • 24
  • Has anyone had any success, or otherwise, getting this approach approved by App Store review? It's been suggested to me that it will not as it is modifying the value through KVO rather than through a public API. – JoGoFo Feb 24 '16 at 05:03
2

There's no way you can force your device into portrait mode if it's in landscape and vice-versa.All you can do is tell your application to be in a particular mode either landscape or portrait.you can do that by:-

  1. You should implement shouldAutorotateToInterfaceOrientation:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    { 
        // Return YES for supported orientations
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    
  2. Editing pList.

    add a key UIInterfaceOrientation into your plist and change its property to UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight, UIInterfaceOrientationPortraitUpsideDown according to your needs..

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
1

Edit

This may not work in iOS 7 but it worked in previous versions of iOS, so downvoting this answer now doesn't make sense. The question was asked 2years ago and I answered it at that time and it was a working solution then.


[[UIDevice currentDevice] setOrientation: UIDeviceOrientationLandscapeLeft];
Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184