how to lock portrait orientation for only view in ios 7

Stefano picture Stefano · May 5, 2014 · Viewed 14.3k times · Source

I have created an application for Iphone and Ipad that is composed from two main views with navigation controller. The navigation controllers are inserted into tabBar controller. I would like lock the main views to Portrait orientation and only a subview of a navigation controller trigger the possibility orintation to Partrait and Landscape. Is it possible? How Can I do?

Thanks

Answer

Duncan C picture Duncan C · May 5, 2014

Both answers given so far are wrong.

Here's what you do:

Make sure you list portrait and landscape in the list of supported orientations in your info.plist file. (The default app templates include all orientations for iPad, and all but portrait upside-down for iPhone, which is probably what you want.)

You want to implement the method supportedInterfaceOrientations: in the view controllers that you want to limit to portrait:

- (NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskPortrait;
} 

Any view controllers that include that code will only support portrait. All others will support all the orientations listed in your info.plist.

The older method shouldAutorotateToInterfaceOrientation: is only needed if you support OS versions prior to 6.0.

You also only need to implement the method shouldAutorotate if you might return NO (don't rotate) sometimes.)