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

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

问题描述

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



优先:

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

但屏幕仍然会旋转到横向。



第二个:
我创建了一个自定义视图控制器类作为 PortraitViewController ,并在 PortraitViewController中添加了以下代码。 m

  @interface PortraitViewController()
@end

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

- (NSUInteger)supportedInterfaceOrientations
{
//这里检查类名,然后返回方向类型
返回UIInterfaceOrientationMaskPortrait;
}
@end

之后我实施了 PortraitViewController.h 作为基类

  #import< UIKit / UIKit.h> 
#importPortraitViewController.h
@interface登录:PortraitViewController
@end

根本不起作用,仍允许视图控制器以横向模式旋转。



我使用的是iOS 8&不希望viewcontroller以横向模式旋转?



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

解决方案

尝试继承 UINavigationController 您正在使用,因为默认的 UINavigationController 没有将 shouldAutorotate 方法转发给您的viewcontroller。 / p>

UINavigationController 子类中实现以下方法

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

现在 UINavigationController 将方法调用转发到其当前可见的 UIViewController ,因此您需要单独实现 shouldAutorotate 以获得所需的效果。


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:

first:

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

but screen still rotates to landscape.

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

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.

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

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

解决方案

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

Implement the following method in your UINavigationController subclass

- (BOOL)shouldAutorotate
{
    return [self.visibleViewController 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的界面方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆