How to lock orientation in UINavigationControllers

情到浓时终转凉″ 提交于 2019-12-23 02:36:26

问题


As the ShouldAutorotateToInterfaceOrientation is deprecated in iOS 6 I am not able to lock the orientation in my app. In my app I have UINavigationControllers with multiple views, some views need to support both portrait and landscape, while other views need to support portrait only. How can I over come this problem please suggest me some idea.

Thanks


回答1:


Use this function only work in iOS 6

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    return UIInterfaceOrientationMaskPortrait;

}



回答2:


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;
}



回答3:


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) ); 
}



回答4:


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



来源:https://stackoverflow.com/questions/12600778/how-to-lock-orientation-in-uinavigationcontrollers

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