【问题标题】:Force landscape for one view controller ios一个视图控制器ios的强制横向
【发布时间】:2013-08-26 21:18:09
【问题描述】:

我查看了几个类似问题的答案,但没有一个答案有效。我有一个应用程序,除了我拥有的一个照片查看器之外,我需要所有肖像。在目标菜单的支持方向部分中,我只有纵向。我如何强制我的一种观点是风景。它正在从导航控制器推入堆栈,但我正在使用情节提要来控制这一切。

【问题讨论】:

  • shouldAutoRotate, [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];, 纳达
  • 您的目标是什么版本的 iOS?
  • 对不起 iOS 6... 我也试过这个:http://*.com/questions/14633213/force-portrait-orientation-while-pushing-from-landscape-view-controller
  • 另外,我读过你的书。很荣幸有你帮助我!
  • 另外,谢谢!那条评论让我度过了一个夜晚。

标签: iphone ios uinavigationcontroller orientation viewcontroller


【解决方案1】:

是的,这是可能的,您可以使用以下代码:

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

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
        return orientation==UIInterfaceOrientationMaskLandscape;
    }

在你的应用委托中试试这个方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (sglobalorientation isEqualToString:@"AllOrientation"]) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

在移动到该景观视图控制器之前,您需要将变量值 sglobalorientation 更改为该字符串值 AllOrientation

在你的景观视图控制器中,在你的视图中使用这个代码将会出现

 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
    DigitalSignatureViewController *digisign = [[DigitalSignatureViewController alloc]init];
    [self presentModalViewController:digisign animated:NO];
    [self dismissModalViewControllerAnimated:NO];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

当您移动到下一个视图控制器时,再次更改 sglobalorientation 字符串值并在您的下一个视图控制器中执行相同的步骤。

【讨论】:

  • “-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window”没什么意义。相反,在 ViewController 中将其实现为“- (NSUInteger)supportedInterfaceOrientations”。
【解决方案2】:

由于答案似乎隐藏在问题的 cmets 中,而且由于 ArunMak 的答案相当混乱,我只提供我发现的内容:

我所要做的就是将此函数添加到视图的自定义 UIViewController 子类中:

- (NSUInteger)supportedInterfaceOrientations {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        // iPad: Allow all orientations
        return UIInterfaceOrientationMaskAll;
    } else {
        // iPhone: Allow only landscape
        return UIInterfaceOrientationMaskLandscape;
    }
}

请注意,该项目需要允许所有方向(即:纵向、左侧横向、右侧横向 - 但在 iPhone 上切勿颠倒!)。

如果您想将部分或大部分视图限制为肖像,您需要在每个视图控制器中实现上述方法(或为其使用公共超类并从其子类化所有其他视图) - 如果您限制设备Info.plist 中的方向只是纵向,应用程序永远不会考虑进入横向。

【讨论】:

    【解决方案3】:

    让我们试试这段代码:

        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
    self.navigationController.view.center = CGPointMake(([UIScreen mainScreen].bounds.size.width/2), [UIScreen mainScreen].bounds.size.height/2);
    CGFloat angle = 90 * M_PI / 180;
    self.navigationController.view.transform = CGAffineTransformMakeRotation(angle);
    self.navigationController.view.bounds = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.height , [UIScreen mainScreen].bounds.size.width);
    

    【讨论】:

    • 这个解决方案是错误的。当您尝试编辑字段时,键盘不会旋转。当然,系统警报也会发生同样的情况。状态栏也不起作用。