如何在iOS 7和iOS 8中锁定设备方向

3

我有一个应用程序的问题。我无法锁定我的应用程序的方向。我需要做的就是将一个视图控制器锁定为横向模式,其他的则为纵向。

这是我的应用程序的层次结构。

*Navigation Controller
     *TabBarController
          *ViewControllers

你的代码在哪里? - Raptor
这是我使用的代码-(BOOL)shouldAutorotate{ return NO; }
  • (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskLandscape; }
  • (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationLandscapeLeft; }
- user3205472
你可以编辑你的问题来发布代码。 - Raptor
还应阅读此内容: https://dev59.com/1ITba4cB1Zd3GeqP630l - Raptor
3个回答

0

你只需要在shouldAutorotate方法中返回NO,并在supportedInterfaceOrientation方法中返回横向的方向,以使其处于横屏状态。

另外,在shouldAutorotate方法中也要返回NO,并从supportedInterfaceOrientation方法中返回纵向的方向掩码。

在所有的视图控制器中:

 -(BOOL)shouldAutorotate { 
     return NO; 
 } 

在你想要的那个方向上:
- (NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskLandscape; 
} 
-    (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    return UIInterfaceOrientationLandscapeLeft; 
} 

在控制器中,您希望以纵向显示:

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

请注意,iOS 7和8使用不同的方法来确定UI方向。 - Raptor
我尝试了这段代码:
  • (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait;
}它可以工作,但是如何让其中一个视图控制器可以横屏呢?
- user3205472
这就是我说的。你必须在你的UIViewControllers中实现我告诉你的方法。 - David Ansermot
我该怎么做?你能教我吗?抱歉,我是新手。非常感谢。 - user3205472
它仍在旋转。我的代码有什么问题? - user3205472

0

使用以下两种方法将设备方向锁定为横屏。

- (NSUInteger)supportedInterfaceOrientations{
[super supportedInterfaceOrientations];
return (UIInterfaceOrientationMaskLandscapeLeft |  UIInterfaceOrientationMaskLandscapeRight);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
{
    return YES;
}
// Return YES for supported orientations
return NO;
}

它能够工作,但我的问题是,我的视图仍然在旋转,如何将方向锁定在特定视图中,其余视图为纵向。谢谢。 - user3205472

0

使用 NavigationController

-(BOOL)shouldAutorotate 
-(NSUInteger)supportedInterfaceOrientations
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

如果您使用“show segue(push)”方法,则永远不会调用这些方法。

请改用“showDetail”方法而非“show”方法。


嗨!谢谢您的回复,但我在这个项目中没有使用故事板。我该怎么做? - user3205472

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接