简体   繁体   中英

changing device orientation to landscape programmatically doesn't work

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

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.

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"];

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..

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM