Custom navigationItem button with QLPreviewController in iOS6

后端 未结 5 1125
悲哀的现实
悲哀的现实 2020-12-09 12:06

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 tha

相关标签:
5条回答
  • 2020-12-09 12:34

    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.

    0 讨论(0)
  • 2020-12-09 12:37

    I tried for a long time to replace this button. Looked to subviews, etc. Looks like this actions/share button is put as a layer to the nav. bar. I ended up by solving this issue for myself by adding one more button instead of title in QLPreviewController subclass.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // Button in center of Navigation Bar
        UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:LS(@"Save"), nil]];
        button.frame = CGRectMake(0, 0, 100, 30);
        button.center = self.view.center;
        button.momentary = YES;
        button.segmentedControlStyle = UISegmentedControlStyleBar;
        button.tintColor = [UIColor colorWithHue:0.6 saturation:0.33 brightness:0.69 alpha:0];
        [button addTarget:self action:@selector(saveToDocumentsClicked) forControlEvents:UIControlEventValueChanged];
        self.navigationItem.titleView = button;
    }
    
    0 讨论(0)
  • 2020-12-09 12:41

    The best way is to implement your own controller and use view of QLPreviewController as subview. In that case you can made your own navigation bar with custom items.

    0 讨论(0)
  • 2020-12-09 12:49

    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...

    0 讨论(0)
  • 2020-12-09 12:55

    It works if you are changing the action item button in top toolbar you would have to do it inside

    -(void)viewDidAppear:(BOOL)animated{
        //add code necessarry to change your action buttons of toptoolbar in quicklook
    }
    

    After the view appears rightBarButtonItems can be accessed.

    0 讨论(0)
提交回复
热议问题