3

我的应用程序中有 UiNavigationController。我希望只有一个屏幕能够旋转,所以我放了这个类:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

-(BOOL)shouldAutorotate {
    return YES;
}

-(NSInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

但发生的问题是在 ecery screen 应用程序发生旋转。我怎样才能禁用它?

4

5 回答 5

10

对于 iOS 6,我在我的应用程序中使用以下代码,它允许您单独指定每个视图控制器的旋转:

AppDelegate.m -

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{NSUInteger orientations =UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}

ViewController.m -

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

最初的代码学分我相信转到 Ray Wenderlich “iOS 6 by Tutorials”一书。雷·温德利希网站

于 2013-01-27T14:35:18.457 回答
1

Use the below code in the class you want to autorotate only:

@interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotate;
@end


@implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = (UINavigationController *) self.selectedViewController;
        if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
            return YES;
    }
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = (UINavigationController *) self.selectedViewController;
        if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
            return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = (UINavigationController *) self.selectedViewController;
        if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
            return YES;
    }
    return NO;

}

@end

Use this code in the parent View Controller of the class (i.e. the just previous class on the stack of navigation controller) that is to be rotated.

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
于 2013-01-28T06:30:07.733 回答
0

在您的 appDelegate 添加此代码

@property(nonatomic,assign)BOOL shouldRotate;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    shouldRotate=NO;
}
-(void)shouldAutoRotate:(BOOL)rotate
{
    self.shouldRotate=rotate;
}

并在您的 rootview 控制器中添加此代码

#import "AppDelegate.h"
#define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]

- (NSUInteger)supportedInterfaceOrientations
{
 if(![myAppDelegate shouldRotate])
     return UIInterfaceOrientationMaskPortrait;
 else
     return UIInterfaceOrientationMaskAllButUpsideDown;
}

之后在 viewcontroller.m 中添加此代码,这是您要旋转的

- (void)viewDidLoad
{
    [super viewDidLoad];
    [myAppDelegate shouldAutoRotate:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [myAppDelegate shouldAutoRotate:NO];
}

我已经为我的一个项目(IOS 7)做了这个。它非常适合我。

于 2014-01-12T09:37:21.927 回答
-1
-(NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;

}
于 2013-01-27T13:41:12.940 回答
-1

您可以创建一个类别来覆盖 navigationController 中的方法以支持所有类。

@implementation UINavigationController (Rotation_IOS6)

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

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end

如果您想限制不同控制器中的旋转,则在相应的视图控制器中覆盖supportedInterfaceOrientations 和shouldAutorote,根据需要更改返回值。

于 2013-01-27T12:20:36.210 回答