2

如何隐藏打印按钮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中,它没有。

4

2 回答 2

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];
    }
}
于 2013-02-28T11:09:56.890 回答
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;

                    }

               }

          }
于 2012-11-13T12:11:44.637 回答