如何强制或禁用某些但不是所有 UIViewController 的界面方向?

How to force or disable interface orientation for some but not all UIViewController?(如何强制或禁用某些但不是所有 UIViewController 的界面方向?)

本文介绍了如何强制或禁用某些但不是所有 UIViewController 的界面方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 9-10 个屏幕的应用.我将 UINavigationController 嵌入到我的视图控制器中.我有几个视图控制器,我只想设置纵向:这意味着旋转设备不应将这些视图控制器旋转到横向模式.我尝试了以下解决方案:

I have an app with 9-10 screens. I embedded a UINavigationController into my view controller. I have few view controllers which I want set only portrait orientation: it means that rotating the device should not rotate these view controllers to landscape mode. I have tried the following solutions:

第一:

   NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
   [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

但屏幕仍会旋转为横向.

but screen still rotates to landscape.

第二:我创建了一个自定义视图控制器类作为 PortraitViewController 并在 PortraitViewController.m

Second: I created a custom view controller class as PortraitViewController and added the code below in PortraitViewController.m

@interface PortraitViewController ()
@end

@implementation PortraitViewController
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    //Here check class name and then return type of orientation
    return UIInterfaceOrientationMaskPortrait;
}
@end

之后我将 PortraitViewController.h 实现为基类

After that I implemented PortraitViewController.h as a base class

#import <UIKit/UIKit.h>
#import "PortraitViewController.h"
@interface Login : PortraitViewController
@end

它根本不起作用,仍然允许视图控制器在横向模式下旋转.

It does not work at all, still allows view controller to rotate in landscape mode.

还有其他我正在使用 iOS 8 的解决方案吗?不想让 viewcontroller 在横向模式下旋转?

Is there any other solution i am using iOS 8 & don't want viewcontroller to rotate in landscape mode?

是否可以只为某些视图控制器设置横向方向,并强制其他视图控制器方向坚持纵向?

Is it possible to have Landscape orientation only for some view controllers, and force other view controllers orientation to stick to Portrait?

推荐答案

尝试继承你正在使用的 UINavigationController 因为默认的 UINavigationController 不转发 视图控制器的 shouldAutorotate 方法.

Try to subclass the UINavigationController you are using because the default UINavigationController is not forwarding the shouldAutorotate method to you viewcontroller.

在您的 UINavigationController 子类中实现以下方法

Implement the following method in your UINavigationController subclass

- (BOOL)shouldAutorotate
{
    return [self.visibleViewController shouldAutorotate];
}

现在 UINavigationController 将方法调用转发到它当前可见的 UIViewController 所以你需要单独实现 shouldAutorotate 以获得你想要的效果.

Now the UINavigationController forwards the method call to its current visible UIViewController so you need to implement shouldAutorotate there individually to get your desired effect.

这篇关于如何强制或禁用某些但不是所有 UIViewController 的界面方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何强制或禁用某些但不是所有 UIViewController 的界面方向?

基础教程推荐