Is there a way to force the interface orientation to change in iPhone?
Asked Answered
M

5

12

I know how to allow/disallow the orientation to rotate using

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

However, I have the problem now where the phone might be in portrait (or upsidedown) and at some point I want to rotate the screen as if the user rotated to landscape. At this point, I don't want autorotate to work anymore. I want to force the interface orientation to stay in landscape. Is there a way to do this? I can probably figure out a hack for turning off the autorotate, but forcing the rotation in the first place I have no idea how to do.

Here's what I'm trying to do:

  • The app rotates to any and all orientations. Everything is normal.

  • An event occurs.

  • Now the autorotate only works for landscapeleft and landscaperight. Moreover, if the user is in portrait or portraitupsidedown, I programmatically rotate to landscaperight to lock the user into that orientation.

Just to make things trickier, I want to pop up a UIAlertView (I know how to do that) before the forced rotation, letting the user know what's going on, and I want to screen behind the alertView to rotate but not the alertView. Is this even possible?

Thanks.

Mintz answered 14/6, 2011 at 17:6 Comment(0)
P
1

The best solution for your situation is to notify the user to turn it to landscape orientation and pause the program until the user turns the orientation. And then when the user rotates the device you can handle the delegate methods and start displaying the screen in the landscape mode. This approach has been handled in many of the app store applications and looks like it is one of the best ways to get the app approved in app store. I hope it makes sense.

Peddler answered 10/4, 2013 at 9:24 Comment(0)
A
0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
    }


    // orientation view swapping logic
- (void)didRotate:(NSNotification *)notification {


    UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
    if (newOrientation != UIDeviceOrientationUnknown || newOrientation != UIDeviceOrientationFaceUp || newOrientation != UIDeviceOrientationFaceDown) 
    {
           orientation = newOrientation;

    }

    // Write your orientation logic here
        if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) 
    {
     // Clear the current view and insert the orientation specific view.
         [self clearCurrentView];
         [self.view insertSubview:yourLandscapeView atIndex:0];

    } else if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) 
    {
           // Clear the current view and insert the orientation specific view.
            [self clearCurrentView];
            [self.view insertSubview:yourPortraitView atIndex:0];

     }
}

and add that notification in the event

Amazon answered 19/3, 2013 at 8:29 Comment(0)
B
0

You cannot. At least not in a way that Apple will allow. The autorotation is designed to be the same for the whole application. You can make code that rotates the view manually, but as far as UIDevice is concerned you'll be in the same orientation.

Benares answered 26/3, 2013 at 14:11 Comment(0)
S
0

According to Apple, if you do not want the default behavior provided by the device orientation rotation API:

You can take control over:

  • The orientations supported by your app
  • How a rotation between two orientations is animated on screen.

The way I read this is "You can control either of these things, but nothing else" or "You're allowed to control only these two things."

I do not see anything in the documentation that explicitly tells you how to "force" the orientation to change. All of that is handled by the device so as it seem more natural and friendly to the person holding the device. Does that make sense?

See the docs here on rotation changes:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html

Finally, I would say this is not possible, and further that it would not be approved by Apple. I up voted Praveen's answer because I think you may need to consider a different approach for what you're trying to do.

Sandie answered 14/4, 2013 at 18:53 Comment(0)
W
-2

yes you ca do that. what you have to do is in -(void) viewwillappear method simply include the following code

if (self.interfaceOrientation == <current orientation>) {
        [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)<to which orientation you want to change.>];
    }

it will solve your problem

Worse answered 5/7, 2012 at 7:19 Comment(2)
This API is hidden, the orientation property for the UIDevice instances is read-only, so it may cause you problems during the review process by Apple.Butts
For <to which orientation you want to change.>, the type of UIInterfaceOrientation cannot be coerced to id. What's the workaround?Linkboy

© 2022 - 2024 — McMap. All rights reserved.