tags:

views:

18372

answers:

7

Okay, I've got my normal app which is in portrait mode. I can force my app to go to landscape mode for a view (using navigationcontroller and viewcontroller) like this:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}

But then when I go back to the main menu (tableview) it goes straight back to portrait. I try this code:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

But that doesn't work..

Any ideas?

A: 

You need to edit your Info.plist file to add the UIInterfaceOrientation key with the appropriate value (UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft).

Is that just to go back to a portrait table main screen? because i want everything portrait, just one view landscape. I can get it landscape but then when i go back i have to rotate the phone to get it back to portrait. I don't want users having the rest landscape.
Domness
+1  A: 

That just makes it start off in landscape.. I don't want that. I want the first and all pages portrait, except for ONE view and go back to portrait after this has closed.

Domness
+4  A: 

To get this to work, I added this to the rootviewcontroller:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}

This seems to be working now.

Domness
Does this count as using private API? orientation is documented as a readonly property.
lawrence
It's all fine via Apple :)
Domness
this line can only rotate the simulator. in logic, you cannot use programming scripts to make users to rotate their iPhones; you can only give hints to users.
Shivan Raptor
Actually Shivan Raptor is wrong.So long as you set shouldAutoRotateTOInterfaceOrientation to only accept the orientation you have forced the device to change to It will remain in that orientation (EVEN ON A DEVICE).I recommend undoing this orientation change in ViewWillDisappear.
Craig Warren
A: 

Here's what I'm doing to do this:

first, put this define at the top of your file, right under your #imports:

#define degreesToRadian(x) (M_PI * (x) / 180.0)

then, in the viewWillAppear: method

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];  
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { 
 self.view.transform = CGAffineTransformIdentity;
 self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
 self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}

if you want that to be animated, then you can wrap the whole thing in an animation block, like so:

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];  
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { 
 self.view.transform = CGAffineTransformIdentity;
 self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
 self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
[UIView commitAnimations];

Then, in your portrait mode controller, you can do the reverse - check to see if its currently in landscape, and if so, rotate it back to Portrait.

Bdebeez
you are close. self.view.frame = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f); self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90)); self.view.center = CGPointMake(160.0f, 240.0f); we have to set the center of view to the center of screen, and fill up the screen (without the 20px space).
Shivan Raptor
A: 

Hey, I want to do that exact thing in a tab controller view. My default views should be portrait, but I'd like one view to be landscape. It seems like all these various attempts aren't coming through without slightly inconsistent results (in the simulater anyway).

Were you able to accomplish this in a clean elegant way? Did you have to redraw everything manually?

Thanks!

asUwish
+3  A: 

Take a look at the function shouldAutorotateToInterfaceOrientation: in the UIViewController class. This function returns YES if the orientations is supported by your UIView. If you return YES only to the landscape orientation, then the iPhone will automatically be put in that orientation.

The following code should do it. Put it in the UIViewController that controls the view that you want to put in landscape mode.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscape);
}
kkrizka
A: 

You can't use this private API.

There is a solution to do that : it's to use a view controller and adding its view to the window. then in that controller you force landscape in the shouldAutorotate... methode. It works fine, but be sure it's necessary for your project to use that, because it's not very smart  to force the user to turn his iPhone. By the way, here is an example code if you need it.

http://www.geckogeek.fr/iphone-forcer-le-mode-landscape-ou-portrait-en-cours-dexecution.html

Vinzius