1

我看到了这个 stackoverflow 帖子:shouldAutorotateToInterfaceOrientation is not working in iOS 6。答案是添加一个名为 RotationIn_IOS6 的类别。我已经这样做了,不同视图控制器的视图在 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);
    }

}

在这方面需要一些指导。不知道如何解决这个问题。

编辑:

代码3:

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;
}

结果 -> 所有视图控制器都锁定为纵向。无法访问 PortraitUpsideDown。一旦进入我的视图控制器,它就可以旋转,但是当你离开视图控制器时,它会锁定在横向中。

编辑2:

每个视图控制器都包含以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return NO;
}
4

3 回答 3

0

The simplest way is Target->General->Deployment Info and tick the orientations you want. E.g. tick only portrait and the app will not auto rotate.

于 2014-01-05T00:32:16.420 回答
0

为了解决这个问题,我将 UINavigationController 子类化而不是使用类别:

我的导航控制器.h

#import <UIKit/UIKit.h>
@interface MyNavigationcontroller : UINavigationController
@end 

我的导航控制器.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

然后在每个视图控制器覆盖:

- (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;
于 2013-05-31T11:37:06.143 回答
0

无需shouldAutorotateToInterfaceOrientation:在导航控制器类别中覆盖,而是在 iOS 5 中,您的每个视图控制器都应shouldAutorotateToInterfaceOrientation:以支持的方向实现。容器控制器(导航控制器)确定的支持方向仅在iOS6及以上。在较低版本中,它将调用shouldAutorotateToInterfaceOrientation:要显示的子视图控制器的

于 2013-05-31T11:15:36.010 回答