How to lock orientation in UINavigationControllers

狂风中的少年 提交于 2019-12-06 14:43:55

Use this function only work in iOS 6

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    return UIInterfaceOrientationMaskPortrait;

}

You lock the orientation by subclassing UINavigationController.

Here is an excellent link on how to do that.

Then you override the following methods in your subclassed UIViewController to achieve the lock (in this case a portrait lock).

-(BOOL)shouldAutorotate
{
    return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

Add following code in viewDidLoad

UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];

For locking portrait orientation

  • Select attribute inspector tab.
  • Set orientation into portrait.

Add the function

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( UIInterfaceOrientationIsPortrait(interfaceOrientation)); 
}

For locking landscape orientation

  • Select attribute inspector tab.
  • Set orientation into landscape.

Add the function

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( UIInterfaceOrientationIsLandscape(interfaceOrientation) ); 
}

I have found the alternate for ShouldAutorotateToInterfaceOrientation.I will like to suggest you to go through these links -

http://www.roostersoftstudios.com/2012/09/21/ios6-autorotation-changes

Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!