0

What i have:

i have a viewcontroller that supports Portrait and landscape. When in landscape i show a filter view for the content of the portrait.

What i need to do:

When the user selects a filter in the landscape view i want to force rotate the device back to portrait.

I have done some Googling and discovered

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

This is a private API and i dont want to do that. (But excatly what i am after)

In short - on user action i want to force rotate the device. Is there something i am missing from the docs?

Any ideas would be super. Dan

Dan
  • 1,447
  • 2
  • 14
  • 30
  • I think its not really possible to force the device rotation in code. Look at this question:http://stackoverflow.com/questions/7280464/device-orientation-change-in-legal-way-ios-4-0 – NDY Aug 09 '12 at 14:05
  • That what i thought but that question was 12 months old so was hoping this have changed :( – Dan Aug 09 '12 at 14:11
  • 1
    I tried it some weeks ago and also did not find any solution on that :/ – NDY Aug 09 '12 at 14:15
  • You are not supposed to force the user to rotate the device while displaying the very same viewController. There (hopefully) never will be any fix for that. You may only present a new viewController modally, making it independent from all other, existing navigation mechanisms (tabBarController, navigationController) and therefor allowing new limitations on the orientation. – Till Aug 21 '12 at 15:44

2 Answers2

2

Learned this from experience! Do this in your viewDidLoad::

UIViewController *vc = [[UIViewController alloc] init];
[self presentModalViewControllerAnimated:NO];
[self dismissModalViewControllerAnimated:NO];

It will force landscape, provided you return YES for shouldAutorotateToInterfaceOrientation for only landscape. No private APIs!

  • Thanks, however this doesnt work unless in the viewDidLoad - i am after it being on user action. (pressing of a button) – Dan Aug 09 '12 at 19:09
-1

You can try with this If works.

-(void)forceFullyPortraitView{
[self.appDel.navigationController.view removeFromSuperview];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[ [UIApplication sharedApplication].self.delegate.window addSubview:self.appDel.navigationController.view];
self.navigationController.navigationBar.hidden=NO;
}
-(void)forceFullyLandscapeView{
[self.appDel.navigationController.view removeFromSuperview];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[ [UIApplication sharedApplication].self.delegate.window addSubview:self.appDel.navigationController.view];
self.navigationController.navigationBar.hidden=NO;
}

You can set landScepe right or left.

Nikunj
  • 987
  • 11
  • 25