我在iOS 6 SDK中遇到了这个问题:我有一些应该被允许轮换的视图(例如视频视图),有些则不允许 . 现在我明白我必须检查应用程序的Info.plist中的所有方向,然后在每个ViewController中进行排序,应该发生什么 . 但它不起作用!应用程序始终旋转到Info.plist中给出的方向 .
Info.plist的:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
任何不允许旋转的ViewController:
//deprecated
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
观察:App旋转到横向和纵向 . 任何想法为什么或我做错了什么?
干杯,马克
编辑:我的最新调查结果还表明,如果您希望在应用程序中的某个位置进行旋转,则必须激活项目设置或Info.plist中的所有四个旋转方向 . 替代方法是覆盖
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
在你的AppDelegate中,它会覆盖Info.plist . 不再可能只在Info.plist中设置Portrait,然后通过覆盖shouldAutorotateToInterfaceOrientation或supportedInterfaceOrientations在某些ViewController中进行旋转 .
9 回答
如果您的ViewController是UINavigationController或UITabBarController的子级,那么它就是您的问题的父级 . 您可能需要子类化该父视图控制器,只是覆盖您在问题中显示的那些InterfaceOrientation方法
编辑:
仅限肖像TabBarController的示例
除了上面的CSmith的答案之外,UINavigationController子类中的以下代码允许以我期望它首先工作的方式委托给顶视图控制器:
这是CSmith方法的另一种替代方案 .
如果要复制导航堆栈/选项卡栏中的所有视图必须就允许的方向集达成一致的iOS 6之前的行为,请将其放在
UITabBarController
或UINavigationController
的子类中:尝试添加此类别:
对于使用UINavigationController和Swift的人,您可以将此扩展添加到项目中 . 之后,导航控制器将控件委托给其子控制器 .
@CSmith和@EvanSchoenberg的进一步补充 .
如果您有一些旋转的视图,而某些视图没有旋转,您必须创建
UITabBarController
的自定义实例,但仍然让每个UIViewController
决定 .@ Alvivi的答案更新为 Swift 4 .
@Shimanski Artem的答案很好,但我认为使用最顶层(当前可见)控制器是更好的解决方案:
作为备用选项,如果您想在应用中保留iOS6之前的旋转功能:
这是github上的一些有用的代码,可以调用iOS6的方法调用,以便旋转像在iOS4 / iOS4上一样 . 这真的对我有所帮助,因为我正在支持一个真正微观管理轮换的遗留应用程序 . 实现iOS6所需的更改需要做很多工作 . 感谢发布它的用户 .
https://gist.github.com/3725118