iOS - QLPreviewController - 如何阻止 QuickLook 旋转?

发布于 2024-11-06 14:38:33 字数 1529 浏览 0 评论 0原文

我的 QuickLook (QLPreviewController) 几乎按照我想要的方式工作,但由于图像特征,我不希望它旋转为纵向。我在“shouldAutoRotateToInterfaceOrientation”方法中配置它,仅对横向旋转返回 yes(请参阅有关详细信息,请参阅下面的代码)但它仍然旋转为纵向。

注意: shouldAutoRotateToInterfaceOrientation 是一个直接副本,在该项目的所有视图控制器中使用,并且它也在其他视图控制器中工作。

//
//  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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

从此见与不见 2024-11-13 14:38:33

AppDelegate.m中替换

返回UIInterfaceOrientationMaskAll;

返回UIInterfaceOrientationMaskLandscape;

就像这样:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskLandscape;
}

In AppDelegate.m replace

return UIInterfaceOrientationMaskAll;

with

return UIInterfaceOrientationMaskLandscape;

just like this:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskLandscape;
}
卷耳 2024-11-13 14:38:33

根据 iOS 的 ViewController 编程指南,自动旋转大致由最近可见的 ViewController 控制。

在您的情况下,这可能是 QLPreviewController 本身,而不是您的 DocumentViewer。 (而且你说后者的 shouldAutorotateToInterfaceOrientation: 没有被调用,这与这个假设一致)。

因此,自动旋转是由 QLPreviewControllershouldAutorotateToInterfaceOrientation: 方法控制的,在我的一个小实验中,该方法似乎允许除颠倒方向之外的所有方向。

因此,您可以做的是定义 QLPreviewController 的子类,它仅重写 shouldAutorotateToInterfaceOrientation: 就像您在 DocumentViewer 中所做的那样,并使用此子类而不是原始的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

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
满地尘埃落定 2024-11-13 14:38:33

我从来没有找到一个好的答案,所以我最终只使用了 UIWebView。

但我仍在寻找。

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

But I'm still looking.

浅暮の光 2024-11-13 14:38:33

试试这个:

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

Try this:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文