【问题标题】:How to lock device orientation in iOS 7 and iOS 8如何在 iOS 7 和 iOS 8 中锁定设备方向
【发布时间】:2015-04-30 08:29:07
【问题描述】:

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

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

*Navigation Controller
     *TabBarController
          *ViewControllers

【问题讨论】:

  • 你的代码在哪里?
  • 这是我使用的代码 -(BOOL)shouldAutorotate{ return NO; } - (NSUInteger)supportedInterfaceOrientations{ 返回 UIInterfaceOrientationMaskLandscape; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationLandscapeLeft; }
  • 您可以编辑您的问题以发布代码。

标签: ios ios7 ios8 orientation


【解决方案1】:

你只需要从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 方向
  • 我试过这个代码 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait;它可以工作,但我怎样才能只景观一个视图控制器?
  • 我说的是这个。你必须实现我在你的 UIViewControllers 中告诉的方法。
  • 我该怎么做?你能教我怎么做吗?对不起,我是菜鸟。非常感谢。
  • 它仍在旋转。我的代码有什么问题?
【解决方案2】:

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

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

【讨论】:

  • 它可以工作,但我的问题是我的视图仍在旋转我如何才能在某个视图中锁定方向而其余部分是纵向的。谢谢。
【解决方案3】:

使用导航控制器

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

这些方法永远不会被调用,如果你使用'show segue(push)'。

更改 segue 'showDetail' 而不是 'show'

【讨论】:

  • 嗨!感谢您的回复,但我没有在这个项目中使用故事板。我该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多