【问题标题】:Customizing QLPreviewController自定义 QLPreviewController
【发布时间】:2011-06-17 13:30:43
【问题描述】:

我在自定义 QLPreviewController 的外观时遇到问题。

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

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

我的意思是我想在 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 ;博士版: 如何管理自定义我的 modal QLPreviewController 的外观?尤其是navigationBar的tintColor?

非常感谢。

【问题讨论】:

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


    【解决方案1】:

    这可行,但我不知道它是否会被 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】:

      继承 QLPreviewController 并更改 viewDidLoad: 中的 tintColor 等。

      【讨论】:

        【解决方案3】:

        如果您试图在整个应用程序中保持简单的样式,例如 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
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多