自定义QLPreviewController

时间:2011-06-17 13:30:44

标签: iphone objective-c ios xcode cocoa-touch

我在自定义QLPreviewController的外观方面存在问题。

我们可以通过在导航控制器中推送它或在ModalViewController中呈现它来显示QLPreviewController。由于我的navigationController的栏是自定义的(tintColor),我正在推动QLPreviewController来保留我的配色方案。但是当我推送它时,QLPreviewController似乎有一些问题:我需要系统地调用 [qlpvc reloadData] 以便显示我的文件。

在iOS [已删除]中,即使使用reloadData,也不会以推送方式显示任何内容(实际上它会以随机方式显示)。所以我认为只使用可靠的Modal方式会很有趣。

Soooo我的观点是我想在ModalViewController中呈现我的QLPreviewController。它的工作方式很棒,但是我无法自定义viewController外观。

例如,didSelectRowAtIndexPath如果我这样做:

(如果我犯了错误,我没有我的消息来源,请原谅我)

QLPreviewController *qlpvc = [[QLPreviewController alloc] init];  
 qlpvc.dataSource = self; // Data Source Protocol & methods implemented of course  
 No need for delegate in my case so //qlpvc.delegate = self;  
 qlpvc.currentPreviewItemIndex = [indexPath.row];  

 // The following doesn't work :  
 [qlpvc.navigationController.navigationBar setTintColor:[UIColor redColor]];  

 // The following doesn't work too :  
 [qlpvc.modalViewController.navigationController.navigationBar setTintColor:[UIColor redColor]];    

 [self presentModalViewController:qlpvc animated:YES];  
 [qlpvc release];

tl;博士版: 如何设法自定义模态 QLPreviewController的外观?特别是navigationBar的tintColor?

非常感谢。

3 个答案:

答案 0 :(得分:3)

子类QLPreviewController并更改viewDidLoad:中的tintColor等。

答案 1 :(得分:3)

这样可行,但我不知道它是否会被Apple拒绝,因为它不是已发布的方法,可能会在未来的操作系统版本中中断。适用于iOS6。

添加到预览控制器数据源方法:

- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    for (id object in controller.childViewControllers)
    {
        if ([object isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *navController = object;
            navController.navigationBar.tintColor = [UIColor colorWithRed:0.107 green:0.360 blue:0.668 alpha:1.000];
        }
    }

    NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"MyPDFFile" ofType:@"pdf"];
    return [NSURL fileURLWithPath:pathToPdfDoc];
}

答案 2 :(得分:0)

如果您尝试在整个应用程序中维护简单样式(如tintColor),则应考虑在许多UIView类上使用UIAppearance选择器。以下示例自定义UINavigationBar的所有实例,包括QLPreviewController中显示的实例:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    //..

    [self initAppearance];

    return YES;

}

-(void)initAppearance{

    UINavigationBar* defaultNavigationBar = [UINavigationBar appearance];

    UIImage *backgroundImage = [UIImage imageNamed:@"MY_IMAGE.png"]

    NSDictionary *defaultNavigationBarDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                    [UIFont fontWithName:@"Futura-Medium" size:19], NSFontAttributeName,
                                                    [UIColor blueColor], UITextAttributeTextColor,
                                                    [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f], UITextAttributeTextShadowColor,
                                                    [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 2.0f)], UITextAttributeTextShadowOffset,
                                                    nil];
    defaultNavigationBar.titleTextAttributes = defaultNavigationBarDictionary;  //iOS5

    //[defaultNavigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];  //iOS5
    [defaultNavigationBar setBarTintColor:[UIColor redColor]];  //iOS7

    [defaultNavigationBar setShadowImage:[[UIImage alloc] init]];  //iOS6, removes shadow
    [defaultNavigationBar setTitleVerticalPositionAdjustment:0.0f forBarMetrics:UIBarMetricsDefault];  //iOS5
    [defaultNavigationBar setBackIndicatorImage:[UIImage imageNamed:@"BACK_ARROW.png"]];  //iOS7
    [defaultNavigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"BACK_ARROW.png"]];  //iOS7

}
相关问题