How to force or disable interface orientation for some but not all 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 的界面方向?


基础教程推荐
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01