问题
How can I hide the print button in QLPreviewController
In IOS5
, this code works
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.delegate = self;
previewController.currentPreviewItemIndex = _fileidx;
[[self navigationController] pushViewController:previewController animated:YES];
[previewController.navigationItem setRightBarButtonItem:nil];
but in IOS6, it does`t.
回答1:
i managed to do it by creating a timer to check for the navigation item and remove it
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];
}
}
回答2:
The issue is that the button seems to be regenerated once the document is ready. I cannot really define "ready" but I wrote a test app and noticed a couple of things:
- Setting the NavigationItem's right bar buttons doesn't work in iOS6
- Scanning the view hierachy and search for an instance of UIToolbar and set the toolbar's buttons works.
The above hack only works if the document being shown is "ready" fast enough. Big documents take longer. I came up with a two step solution:
- Search the navigation item
- Constantly hide it using NSTimer.
Here's some code:
This is from ViewDidAppear():
Setup a timer that keeps on hiding the button.
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 );
} );
}
This searches the navigation item:
/// <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;
}
}
}
来源:https://stackoverflow.com/questions/13083546/qlpreviewcontroller-hide-print-button-in-ios6