PROGRAMMATICALLY ROTATING THE SCREEN
You've seen how your application can handle changes in device orientation when the user rotates the device. Sometimes (such as when you are developing a game), however, you want to force the application to display in a certain orientation independently of the device's orientation.
There are two scenarios to consider:
- Rotating the screen orientation during runtime when your application is running
- Displaying the screen in a fixed orientation when the View window has been loaded
Rotating during Runtime
During runtime, you can programmatically rotate the screen by using the setOrientation: method on an instance of the UIDevice class. Suppose you want to let users change the screen orientation: They press the Round Rect Button. Using the project created earlier, you can code it as follows (you need to connect the Touch Up Inside event of the button to this IBAction):
-(IBAction) btnClicked: (id) sender{
[[UIDevice currentDevice]
setOrientation:UIInterfaceOrientationLandscapeLeft];
}
The setOrientation: method takes a single parameter specifying the orientation to which you want to change.
NOTE After you have programmatically switched the orientation of your application, your application's rotation can still be changed when the device is physically rotated. The orientation that it can be changed to is dependent on what you set in the shouldAutorotateToInterfaceOrientation: ...