5

I have QuickLook (QLPreviewController) almost working how I want it, but because of the images characteristics I don't want it to rotate into portrait orientation.I have it configured in the "shouldAutoRotateToInterfaceOrientation" method to only return yes for landscape rotations (see code below for details) but it is still rotating to portrait.

Note: The shouldAutoRotateToInterfaceOrientation is a direct copy that is used in all of my view controllers for this project and it is working in the other view controllers.

//
//  documentViewer.m
//

#import "DocumentViewer.h"

@implementation DocumentViewer

@synthesize documents;

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        return YES;
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return YES;
    else 
        return NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

//-(void)viewWillAppear:(BOOL)animated {
//  
//  self.userInteractionEnabled = YES;
//}

//Nessary for Enabling User Interaction
- (BOOL)canBecomeFirstResponder {
    return YES;
}

-(void) createList:(NSString *) document {

    documents =     [[NSArray arrayWithObjects:document, nil] retain];
}

-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

    return [documents count];
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {

    return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}
@end
Mytheral
  • 3,929
  • 7
  • 34
  • 57

4 Answers4

5

In AppDelegate.m replace

return UIInterfaceOrientationMaskAll;

with

return UIInterfaceOrientationMaskLandscape;

just like this:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskLandscape;
}
Carlos Dias
  • 131
  • 1
  • 7
  • This works as it makes ALL views. Jörn Eyrich's solution above allows you to affect just one class of view. However, two good answers :-) – ferdil Nov 17 '14 at 10:54
2

According to the ViewController Programming Guide for iOS, the autorotation is roughly controlled by the ViewController that was most recently made visible.

In your case that's probably the QLPreviewController itself, not your DocumentViewer. (And you say that the latter's shouldAutorotateToInterfaceOrientation: isn't called, which is consistent with this hypothesis).

So the autorotation is controlled by the shouldAutorotateToInterfaceOrientation: method of QLPreviewController, which in a little experiment of mine seems to allow everything but upside-down orientation.

So what you can do is define a subclass of QLPreviewController that only overrides shouldAutorotateToInterfaceOrientation: the way you did in DocumentViewer and use this subclass instead of the original QLPreviewController.

LandscapeOnlyQLPreviewController.h:

#import <QuickLook/QuickLook.h>

@interface LandscapeOnlyQLPreviewController : QLPreviewController {
}
@end

LandscapeOnlyQLPreviewController.m:

#import "LandscapeOnlyQLPreviewController.h"

@implementation LandscapeOnlyQLPreviewController
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
  return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
@end
Jörn Eyrich
  • 5,141
  • 1
  • 19
  • 14
  • Doesn't seem to work... copied what you had verbatim and changed my DocumentList.m to use the new LandscapeOnlyPreviewController. My guess is that when LandScapeOnlyQLPreviewController calls back to the base class the base class then launches a brand new UIViewController which I have no control over, unless I wish to change apple's code... Or did I completely miss something from your post? – Mytheral May 19 '11 at 12:04
  • I think that's unlikely. Can you put an NSLog() into the `shouldAutorotateToInterfaceOrientation:` of `LandscapeOnlyQLPreviewController` and see if it gets called at all? Can you post the code of the action method that gets called when the user hits the "more detail" button that you mention above? – Jörn Eyrich May 19 '11 at 19:23
  • Yup, One of the first things I did... I put a breakpoint in there, then an NSLog and it never even was called... Although DeAlloc is. – Mytheral May 19 '11 at 19:37
  • Weird... can you see if the `viewWillAppear:` method etc. are getting called? Are you pushing the `QLPreviewController` on the NavController's stack? Or are you just getting its view and adding it as a subview somwehere in your hierarchy so that the controller's lifecycle methods are bypassed? I still think posting the action method's source could help. – Jörn Eyrich May 19 '11 at 20:08
  • If someone is still looking for a solution. I could achieve the desired behavior by implementing `supportedInterfaceOrientations` returning `UIInterfaceOrientationMaskLandscape` instead of the `shouldAutorotateToInterfaceOrientation`. – Traspler Feb 23 '15 at 12:25
1

I never did find a good answer, so I ended up just using a UIWebView.

But I'm still looking.

Mytheral
  • 3,929
  • 7
  • 34
  • 57
-1

Try this:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Moshe
  • 57,511
  • 78
  • 272
  • 425