简体   繁体   English

如何在UINavigationControllers中锁定方向

[英]How to lock orientation in UINavigationControllers

As the ShouldAutorotateToInterfaceOrientation is deprecated in iOS 6 I am not able to lock the orientation in my app. 由于在iOS 6中不推荐使用ShouldAutorotateToInterfaceOrientation ,因此无法在我的应用中锁定方向。 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. 在我的应用程序中,我有UINavigationControllers有多个视图,一些视图需要支持纵向和横向,而其他视图只需要支持肖像。 How can I over come this problem please suggest me some idea. 我怎么能过来这个问题请给我一些想法。

Thanks 谢谢

Use this function only work in iOS 6 使用此功能仅适用于iOS 6

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    return UIInterfaceOrientationMaskPortrait;

}

You lock the orientation by subclassing UINavigationController. 您可以通过继承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). 然后,在子类UIViewController中覆盖以下方法以实现锁定(在本例中为纵向锁定)。

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

Add following code in viewDidLoad 在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 - 我找到了ShouldAutorotateToInterfaceOrientation的替代品。我想建议你仔细阅读这些链接 -

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

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM