Teo
Teo

Reputation: 3442

iOS change screen orientation on demand

I have an app with an UITableView at the home screen. I made this view to always be in landscape orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
     return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

If the user picks a row he will be taken to a UIPageViewController view. This second view can rotate to landscape and portrait. The problem is when I am in portrait mode on the UIPageViewController and I press the go back button, the first view which is supposed to be always in landscape mode now is in portrait. After I rotate it, it gets into landscape and stays there.

I was wondering maybe if there is a way to make my home screen go automatically into landscape when I go back to it.

Upvotes: 0

Views: 2017

Answers (6)

user2319290
user2319290

Reputation: 7

Use this, change the UIInterfaceOrientationLandscapeLeft to required orientation type as UIDeviceOrientationPortrait, UIDeviceOrientationLandscapeLeft etc.

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

Upvotes: 0

Henry
Henry

Reputation: 21

If you want to make a screen in a particular orientation then you can create a CustomNavigation controller and then present it in your app. You have to only return supportedInterfaceOrientations in this. If you want more detail and sample code click here.

Upvotes: 1

Harshit Gupta
Harshit Gupta

Reputation: 1210

Try the following

  1. Create the view of your main screen in app in interface builder in Landscape mode.
  2. Create uiview oultlet in interface class and connect it to above view.
    IBOutlet UIVIew *myView;
  3. Then in the viewDidLoad method set this self.view = self.myView;

Upvotes: 2

Teo
Teo

Reputation: 3442

If you use the UINavigationViewController methods(pushViewController:animated: and popViewControllerAnimated:), the views will inherit the previous view's orientation.

On the other hand, if you use presentModalViewController:animated: and dismissModalViewControllerAnimated: methods, everything works perfectly. Hope this helped!

Upvotes: 0

Sreeram
Sreeram

Reputation: 3258

As said in the view controller programming guide, you can have a alternate landscape interface and before coming to home screen from any other view, you can check the orientation and push the corresponding interface onto the screen

Read this SO question and answer for better understanding of launching an app in landscape.Also go through above apple programming guide which i pointed to.

Upvotes: 0

jimpic
jimpic

Reputation: 5520

Call shouldAutorotateToInterfaceOrientation manually when you go back. You can not force a "real" orientation change, that's a OS thing.

Upvotes: 0

Related Questions