QLPreviewController隐藏ios6中的打印按钮

时间:2012-10-26 08:39:25

标签: ios6 qlpreviewcontroller

如何在QLPreviewController

中隐藏打印按钮

IOS5中,此代码有效

QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.delegate = self;
previewController.currentPreviewItemIndex = _fileidx;
[[self navigationController] pushViewController:previewController animated:YES];
[previewController.navigationItem setRightBarButtonItem:nil];

但在IOS6中,它确实没有。

2 个答案:

答案 0 :(得分:2)

我设法通过创建一个计时器来检查导航项并将其删除

myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
             target:self
             selector:@selector(hideRightButton:)
             userInfo:nil
             repeats:YES];
- (void)hideRightButton:(NSTimer *)timer {
    [self inspectSubviewsForView:self.view];
}
- (void)inspectSubviewsForView:(UIView *)view
{
    for (UIView *subview in view.subviews)
    {
        NSLog(@"class detected %@",[subview description]);
        if ([subview isKindOfClass:[UINavigationBar class]])
        {
            UINavigationBar *bar = (UINavigationBar *)subview;
            if ([[bar items] count] > 0)
            {
                UINavigationItem *navItem = [[bar items] objectAtIndex:0];
                [navItem setRightBarButtonItem:nil];
                {
                }

                if ([subview isKindOfClass:[UIView class]] && [[subview subviews] count] > 0)
                {
                    [self inspectSubviewsForView:subview];
                }
            }
        }
        [self inspectSubviewsForView:subview];
    }
}

答案 1 :(得分:0)

问题是文档准备好后,按钮似乎会重新生成。我不能真正定义“准备好”,但我写了一个测试应用程序,并注意到了一些事情:

  • 设置NavigationItem的右侧栏按钮在iOS6中不起作用
  • 扫描视图层次并搜索UIToolbar的实例,并设置工具栏的按钮有效。

上述黑客仅在所显示的文档足够快“准备好”时才有效。大文件需要更长时间。 我想出了一个两步解决方案:

  • 搜索导航项
  • 使用NSTimer不断隐藏它。

以下是一些代码:

这是来自ViewDidAppear():

设置一个不断隐藏按钮的计时器。

NSTimer oTimer = NSTimer.CreateRepeatingTimer(0.2, this.HidePrintButton);
NSRunLoop.Current.AddTimer(oTimer, NSRunLoopMode.Default);

private void HidePrintButton()

{
if(this.oNavItem == null)
{
return;
}
this.InvokeOnMainThread(
delegate {
this.oNavItem.SetRightBarButtonItems( new UIBarButtonItem[0], false );
} );
}

这将搜索导航项:

/// <summary>

          /// Finds the first navigation item inside a view hierachy.

          /// </summary>

          /// <param name='oCurrentView'>the view to start searching from</param>

          /// <param name='oItem'>will be set if the navigation item was found</param>

          public static void FindNavigationItem(UIView oCurrentView, ref UINavigationItem oItem)

          {

               if(oItem != null || oCurrentView == null || oCurrentView.Subviews == null || oCurrentView.Subviews.Length <= 0)

               {

                    return;

               }



               // Check if a UINavigationBar was found. This will contain the UINavigationItem.

               if(oCurrentView is UINavigationBar)

               {

                    UINavigationBar oBar = (UINavigationBar)oCurrentView;

                    if(oBar.Items != null && oBar.Items.Length > 0)

                    {

                         oItem = oBar.Items[0];

                    }

                    return;

               }



               // Recursively loop all sub views and keep on searching.

               foreach (var oSubView in oCurrentView.Subviews)

               {

                    FindNavigationItem(oSubView, ref oItem);

                    if(oItem != null)

                    {

                         break;

                    }

               }

          }