【问题标题】:How to rotate one view controller and others stay in one mode (portrait mode) in ios?如何在 ios 中旋转一个视图控制器而其他视图控制器保持在一种模式(纵向模式)?
【发布时间】:2014-07-10 09:38:37
【问题描述】:

我有五个视图控制器,并且在项目目标设备方向中我启用了纵向、横向左侧和横向右侧。现在我想要 5 个视图控制器中的 4 个视图控制器保持纵向模式(不旋转到横向左侧和横向右侧),并且只有一个视图控制器在所有模式下旋转(纵向、横向左侧、横向右侧)。那么如何做到这一点请告诉。

【问题讨论】:

  • 你之前尝试过什么?

标签: ios iphone objective-c rotation


【解决方案1】:

为每个 ViewController 实现 -(NSUInteger)supportedInterfaceOrientations 并指定每个控制器应支持的界面方向。

编辑

假设您对每个 ViewController 都有单独的实现,请在每个实现中实现 -(NSUInteger)supportedInterfaceOrientations-(BOOL)shouldAutorotate

例如

-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}

将确保您的视图控制器支持所有横向模式。结合这个

-(BOOL)shouldAutorotate{
return YES;
}

你的显示器会在旋转时“翻转”。

使用枚举 UIInterfaceOrientationMask 调整支持的方向,并尝试不同的组合以及对 -(BOOL)shouldAutorotate 的返回值是/否,直到获得所需的行为。

【讨论】:

  • 我已经使用了这个我正在尝试假设我的第一个视图控制器处于横向模式(这个视图控制器旋转上面使用的所有模式)现在当我们推动第二个视图控制器时它也以横向模式打开但我想扩孔以纵向模式打开的所有视图控制器,但这不会发生。
  • 您的每个 ViewController 是否都有自己的实现,还是都使用相同的代码?
  • 不,我为每个视图控制器启用了 3 种模式(上面提到过)现在我只希望一个视图控制器遵循所有模式,并且铰孔视图控制器仅在纵向模式下打开。
  • 请展示您对 -(NSUInteger)supportedInterfaceOrientation 和 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 的实现
  • 我没有使用 -(NSUInteger)supportedInterfaceOrientations 我在目标中使用项目并启用模式。
【解决方案2】:

首先,在 AppDelegate 中,这样写。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
     return UIInterfaceOrientationMaskAll;
}

Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions

- (BOOL)shouldAutorotate
{
     return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
     return UIInterfaceOrientationMaskPortrait;
}
For UIViewControllers, which require LANDSCAPE too, change masking to All.

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
    //OR return UIInterfaceOrientationMaskAll;
}
Now, if you want to do some changes when Orientation changes, then use this function.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

}

请注意:-

很大程度上取决于您的 UIViewController 嵌入到哪个控制器中。

例如,如果它在 UINavigationController 中,那么您可能需要继承该 UINavigationController 以覆盖这样的方向方法。

子类 UINavigationController(层次结构的顶部视图控制器将控制方向。)确实将其设置为 self.window.rootViewController。

- (BOOL)shouldAutorotate
 {
     return self.topViewController.shouldAutorotate;
 }
 - (NSUInteger)supportedInterfaceOrientations
 {
     return self.topViewController.supportedInterfaceOrientations;
 }

从 iOS 6 开始,UINavigationController 不会向其 UIVIewControllers 请求方向支持。因此我们需要对它进行子类化。

【讨论】:

  • 如何使用此代码以及在所有视图控制器或特定视图控制器中使用它的位置。
猜你喜欢
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 2013-05-18
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多