Pregunta

my app doesn't support rotation. But I want to present a QLPreviewController that supports rotation. I present the QLPreviewController like this:

[self presentModalViewController:thePrevController animated:NO];

How can I do this?

¿Fue útil?

Solución

Enable all rotations in your application plist file. This will make all views rotate irrespective of the settings in the view controller.

Then subclass your root UINavigationController as below, adding the rotation control code for iOS5 and 6 depending on your requirements:

I was updating an old app with MainWindow.xib, so I changed the class of the navigation controller in the xib file to CustomNavigationController. But in a more modern app with say a main menu, you'd instantiate the nav controller like this:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

MainMenuVC *masterViewController = [[MainMenuVC alloc] initWithNibName:@"MainMenuVC" bundle:nil];
self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

Subclass UINavigationController

#import <UIKit/UIKit.h>

@interface CustomNavigationController : UINavigationController

@end

#import "CustomNavigationController.h"

@interface CustomNavigationController ()

@end

@implementation CustomNavigationController

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

@end

Then subclass the QLPreview controller so you can override the rotation code which will enable rotation for the QLPreviewController only. The rest of the app with views pushed from your CustomNavContoller will not rotate as the CustomNavigationController is locked.

I added this interface and implementation at the top of the view controller where I wanted to present the QLPreviewController.

@interface RotatingQLPreviewController : QLPreviewController

@end

@implementation RotatingQLPreviewController

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

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

@end

Then present your QLPreviewController using your subclass.

RotatingQLPreviewController *preview = [[RotatingQLPreviewController alloc] init];
        preview.dataSource = self;
        [self presentViewController:preview
                           animated:YES
                         completion:^(){
                             // do more stuff here
                         }];

This method should work for other modal views that you want to rotate, but I haven't tried it.

I implemented this method in the latest app I'm working on and works in both iOS5 and 6.

Hope it helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top