my goal is to use QLPreviewController in my iPad application for iOS6, using my custom Action item button in the top toolbar. I had solution until iOS5.1. I used a class that extends QLPreviewController and during component lifecycle I did something like
[[self navigationItem] setRightBarButtonItems:[NSArray arrayWithObject:[self buildCustomButton]]];
With iOS6 this trick doesn't work more, and now it seems impossible change navigationItem configuration. I think that introduction of UIActivity and Social Framework could be involved and maybe it's no more effective to work on navigationItem, but I can find any solution. Any suggestion? Thanks, bye
I really really needed a solution, so I made this up.
Yes, it's ugly. Yes, it may break at any time. And yes, I'll go strait to dev hell, but my boss did stop stare at me with angry eyes...for now.
@implementation UINavigationItem (Custom)
void MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL);
- (void) override_setRightBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated{
if (item && [item.target isKindOfClass:[QLPreviewController class]] && item.action == @selector(actionButtonTapped:)){
QLPreviewController* qlpc = (QLPreviewController*)item.target;
[self override_setRightBarButtonItem:qlpc.navigationItem.rightBarButtonItem animated: animated];
}else{
[self override_setRightBarButtonItem:item animated: animated];
}
}
+ (void)load {
MethodSwizzle(self, @selector(setRightBarButtonItem:animated:), @selector(override_setRightBarButtonItem:animated:));
}
void MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL) {
Method origMethod = class_getInstanceMethod(c, origSEL);
Method overrideMethod = class_getInstanceMethod(c, overrideSEL);
if (class_addMethod(c, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
class_replaceMethod(c, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
}else{
method_exchangeImplementations(origMethod, overrideMethod);
}
}
@end
Steve Jobs will hunt me in my dreams until I find a proper solution...
Well, i've got good news and bad news.
The good news is i've figured out why this isn't working. In iOS6 the QLPreviewController's navigationItem no longer has a navigationBar:
(lldb) po [[self navigationItem] navigationBar];
(id) $2 = 0x00000000 <nil>
The navigation bar is now located deep within the view hierarchy of the QLPreviewControllersView:
QLPreviewViewController.view->UIView->UIView->QLRemotePreviewContentController->navBar->navItem->rightBarButtonItems.
You can use the below method to find the navigationItem that you're looking for:
- (void)inspectSubviewsForView:(UIView *)view
{
for (UIView *subview in view.subviews)
{
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];
}
}
}
Simply pass [self view] to that method and it will loop until it finds the tab bar in question. You can then remove or add your own.
The bad news is of course that you are accessing private APIs and use of this will likely get your app rejected by the app store. It is however the only answer i've seen on this. Would love to see if there is a non-private way to do this but given the way it's set up, it seems unlikely.
Also, this method will only work if it is called after the bar is already in position. The best place to call this from is the 'viewDidAppear' but it doesn't work 100% of the time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With