【问题标题】:how to customize QLPreviewController's navBar and toolbar tintColor如何自定义 QLPreviewController 的 navBar 和工具栏 tintColor
【发布时间】:2012-11-09 15:44:48
【问题描述】:
QLPreviewController * preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
preview.currentPreviewItemIndex = sender.tag;
preview.editing= YES; 
[self presentModalViewController:preview animated:YES];
[preview release];

这两行对我不起作用。所以在写这些行之前要小心。

[preview.tabBarController.tabBar setTintColor:[UIColor blackColor]];
[preview navigationController].navigationBar setTintColor: [UIColor blackColor]];

【问题讨论】:

    标签: iphone ios ipad uinavigationcontroller uitoolbar


    【解决方案1】:

    从 iOS5 开始,您可以基于实例、全局或包含在特定容器类中时为控件设置主题。从 iOS6 开始,前一种子类化 QLPreviewController 来设置 UINavigationBar 的 tintColor 的方法停止工作。

    考虑以下之一作为与 iOS5 和 iOS6 兼容的解决方法示例:

    QLPreviewController 中包含的任何 UINavigationBar:

    [[UINavigationBar appearanceWhenContainedIn:[QLPreviewController class], nil]
            setTintColor:[UIColor blackColor]];
    

    或全局设置应用程序中所有 UINavigationBar 实例的 tintColor:

     [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
    

    同样的策略也适用于 UITabBarController。

    【讨论】:

      【解决方案2】:

      用这条线设置UINavigationController的样式..

      self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
      

      而要更改 TabBar 的颜色,只需在您的班级的 viewWillAppear 中添加以下代码

      CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
      UIView *v = [[UIView alloc] initWithFrame:frame];
      [v setBackgroundColor:[UIColor colorWithRed:0.1 green:0.2 blue:0.6 alpha:0.8]];
      [v setAlpha:0.5];
      [[self.tabBarController tabBar] insertSubview:v atIndex:0];
      [v release];
      

      【讨论】:

        【解决方案3】:

        如果你想改变 navigationBar 的 tintColor,你可以推送你的 QLPreviewController 来代替模态显示:

        //i assume that you already have a navigationController
        [[self navigationController] pushViewController:previewer animated:YES];
        [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
        

        对于底部栏,我认为这是一个 UIToolbar 而不是 UITabBar,可能你不能改变颜色(我不知道),但你肯定不能打电话给preview.tabBarController.tabBar

        【讨论】:

        • 对 iOS7 Tinting 非常有用!
        【解决方案4】:

        我找到了一个解决方案,虽然它不是正确的方法,但它确实有效:

        创建QLPreviewController的子类

        MyQLPreviewController.h

        @interface MyQLPreviewController : QLPreviewController
        
        @end
        

        在新子类的 .m 中,复制以下代码

        @implementation MyQLPreviewController
        
        - (void)viewWillAppear:(BOOL)animated
        {
            [super viewWillAppear:animated];
        
            UIToolbar *toolbar = [self getToolBarFromView:self.view]; //NOTE: Not the correct apperoach! could not think better solution, as iOS does not allow to access the toolbar properties in QLPreviewController
        
            toolbar.barTintColor = [UIColor redColor];
        
        }
        
        - (UIToolbar *)getToolBarFromView:(UIView *)view
        {
            for (UIView *subView in view.subviews)
            {
                if ([subView isKindOfClass:[UIToolbar class]])
                {
                    return (UIToolbar *)subView;
                }
                else
                {
                    UIToolbar *toolBar = [self getToolBarFromView:subView];
        
                    if (toolBar)
                    {
                        return toolBar;
                    }
                }
            }
            return nil;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-20
          • 2015-05-02
          相关资源
          最近更新 更多