views:

138

answers:

2

Is there to way to fix a view's orientation in a landscape position, regardless of the device's current position?

To be more specific, I have a UIWebView in a UINavigationController that should always show its content as if the device were rotated 90 degress counterclockwise. I only want the web view to be restricted to this orientation; every other view in the app should behave normally.

+2  A: 

Yes. In your info.plist file, define:

UIInterfaceOrientation UIInterfaceOrientationLandscapeRight

EDIT:

The above changes the orientation for the whole app, and you say you don't want that. Can the user move the phone to achieve the orientation you need?

If you don't want to rely on the user to change the phone to that orientation, try:

    self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
mahboudz
I only want to restrict the orientation on this one view, wouldn't changing the info.plist apply it across the whole app?
conmulligan
Yes. See adurdin's answer below. You may have to do a 90 degree transform if the user doesn't move the photo to that orientation.
mahboudz
The transformation works perfectly, thanks for your help.
conmulligan
Yeah, you have to do the transformation, as unless the root view controller of a UINavigationController supports orientation changes, none of the subsequently pushed views will not even get shouldAutorotateToInterfaceOrientation: called.
adurdin
+1  A: 

In the controller for the view, implement shouldAutorotateToInterfaceOrientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
    return UIInterfaceOrientationIsLandscape(orientation);
}

Or if you only want to allow Left Landscape (the default for the Youtube app, for example)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
    return orientation == UIInterfaceOrientationLandscapeRight;
}
adurdin
This doesn't appear to be working; I suspect it's because the WebView is contained in a NavigationController...
conmulligan