0

I am working on ARC based project. My project is targeted iOS 4.3 I want to force the

Portrait orientation to one of my view. The following doesn't seem to work for me.

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];

Any help on this?

Raj
  • 1,119
  • 1
  • 15
  • 32

1 Answers1

1

First of all the viewController to which orientations need to supported must implement the below method passing the supported orientations.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

Later to manually rotate the view set the device orientation directly using

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

if this doesn't work use

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

remember if you return NO for the setting orientation in shouldAutoRotate this may not work. Also check this in both ios5 & ios6, i have tried in ios5.

EDIT: For iOS5 or iOS6 where setOrientation doesn't exist

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];

this works fine, but this may be a private api now.

vishy
  • 3,241
  • 1
  • 20
  • 25
  • neither of the iOS its working.? have you tried both ways..? And one more thing the viewController is a presented one or pushed on a navigation stack.? – vishy Oct 17 '12 at 08:32
  • The view Controller is pushed to the navigation stack – Raj Oct 17 '12 at 08:33
  • does the rootViewController of the navigation stack support the orientation u r setting.? – vishy Oct 17 '12 at 08:45
  • check my edit, your orient methods was right.. where are u calling that method..? in `viewDidLoad` or `viewWillAppear`? – vishy Oct 17 '12 at 09:40
  • I am calling it in ViewDidLoad and the method itself throws an error for me now – Raj Oct 17 '12 at 09:58
  • for non ARC that works, for others look at this post http://stackoverflow.com/questions/12650137/how-to-change-the-device-orientation-programmatically-in-ios-6 the last answer may help you.. – vishy Oct 17 '12 at 10:43