Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK: Orientation (Landscape and Portrait views)

Tags:

cocoa-touch

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?

like image 237
Domness Avatar asked Jan 10 '09 15:01

Domness


People also ask

How do I Turn Off portrait orientation on my iPhone?

1 Swipe down from the top-right corner of your screen to open Control Center. 2 Tap the Portrait Orientation Lock button to make sure that it's off. 3 Turn your iPhone sideways.

How do I Turn Off orientation lock on my iPhone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways. If the screen still doesn't rotate, try a different app — like Safari or Messages — which are known to work in landscape mode.

How do I Fix my iPhone or iPod touch screen orientation problem?

1 Swipe up from the bottom edge of your screen to open Contol Center. 2 Tap the Portrait Orientation Lock button to make sure that it's off. 3 Turn your iPhone or iPod touch sideways. If the screen still won't rotate, try another app — like Safari or Messages — which are known to work in landscape mode. See More....

How do I change the orientation of my screen on iPhone?

Learn more. If you have an iPhone Plus, and want the Home screen to rotate, go to Settings > Display & Brightness and set Display Zoom to Standard. If you have an iPad with a Side Switch, you can set the Side Switch to work as a rotation lock or mute switch. Go to Settings > General.


2 Answers

Take a look at the function shouldAutorotateToInterfaceOrientation: in the UIViewController class. This function returns YES if the orientations are supported by your UIView. If you return YES only to the landscape orientation, 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);
}
like image 100
kkrizka Avatar answered Oct 04 '22 06:10

kkrizka


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.

like image 28
Domness Avatar answered Oct 04 '22 08:10

Domness