我看到了这个stackoverflow帖子:shouldAutorotateToInterfaceOrientation is not working in iOS 6。答案是添加一个名为RotationIn_IOS6的类别。我已经这样做了,不同的viewcontrollers的视图在iOS6中正常工作。问题出在iOS5中。
我只需要几个视图就可以向所有方向旋转,其余的应该是纵向或PortraitUpsideDown。我遇到的问题是,要么全部不旋转(code1),要么旋转到横向,它会保持相同的方向,直到你旋转回肖像(code2)。
代码1:
@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
@end
代码2:
@implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if([self.visibleViewController isKindOfClass:[MyClassToRotate class]])
{
return [self.visibleViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
需要一些指导。不知道如何解决这个问题。
编辑:
CODE3:
1)从类别中删除了shouldAutorotateToInterfaceOrientation。 2)将此代码添加到我的特定类
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//from here you Should try to Preferred orientation for ViewController
return TRUE;
}
结果 - >所有的viewcontroller都锁定在肖像中。无法访问PortraitUpsideDown。一旦进入我的视图控制器,它就可以旋转,但当你离开视图控制器时,锁定在风景中..
编辑2:
每个viewcontroller都包含以下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}
答案 0 :(得分:0)
无需覆盖导航控制器类别中的shouldAutorotateToInterfaceOrientation:
,而不是iOS 5,每个视图控制器都应使用支持的方向实现shouldAutorotateToInterfaceOrientation:
。由容器控制器(导航控制器)确定的支持方向仅在iOS6及更高版本中。在较低版本中,它将调用将要显示的子视图控制器的shouldAutorotateToInterfaceOrientation:
答案 1 :(得分:0)
要解决这个问题,我已经将UINavigationController子类化,而不是使用类别:
<强> MyNavigationcontroller.h 强>
#import <UIKit/UIKit.h>
@interface MyNavigationcontroller : UINavigationController
@end
<强> MyNavigationcontroller.m 强>
#import "MyNavigationcontroller.h"
@interface MyNavigationcontroller ()
@end
//Set your init...
-(BOOL)shouldAutorotate {
if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"YourClass")])
return YES;
}
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"YourClass")]) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortraitUpsideDown;
}
@end
然后在每个viewcontroller中覆盖:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationMaskPortraitUpsideDown);//or your orientation
}
然后,在发布时,在您的应用代理的didFinishLaunchingWithOptions:
中:
NSString *reqSysVer = @"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
self.navigationController = [[MyNavigationcontroller alloc] initWithRootViewController:masterViewController];//IOS 6
}else{
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
}
self.window.rootViewController = self.navigationController;
答案 2 :(得分:0)
最简单的方法是Target-&gt; General-&gt;部署信息并勾选您想要的方向。例如。仅勾选纵向,应用程序不会自动旋转。