JDFTooltips icon indicating copy to clipboard operation
JDFTooltips copied to clipboard

Right way to create BarButtonItem tips

Open anuchandra opened this issue 10 years ago • 7 comments

I'd like to add a tooltip pointing to the right BarButton in the Navigation bar. I can't seem to figure out the right approach.

I've tried this but it doesn't work. I can't get a pointer to the host view. It seems the targetview is nil.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadObjects];
    UIBarButtonItem *rightBarButtonItem = self.navigationItem.rightBarButtonItem;
    UIView *targetView = [self.navigationItem.rightBarButtonItem valueForKey:@"view"];
    UIView *targetSuperview = [targetView superview];
    UIView *containerView = [targetSuperview superview];

    if (nil == containerView) {
        NSLog(@"Cannot determine container view from UIBarButtonItem: %@", self.navigationItem.rightBarButtonItem);
        return;
    }
    JDFTooltipView *tooltip = [[JDFTooltipView alloc] initWithTargetBarButtonItem:self.navigationItem.rightBarButtonItem hostView:containerView tooltipText:@"Right bar button" arrowDirection:JDFTooltipViewArrowDirectionDown width:200.0f];
    [tooltip show];
}

So I tried a different way to get the view for UIBarButtonItem but again the targetview is nil.

UIView *targetView = (UIView *)[self.navigationItem.rightBarButtonItem performSelector:@selector(view)];

I even tried getting the view this way but no luck there either.

UIView* customview = [self.navigationItem.rightBarButtonItem customView];

anuchandra avatar Feb 01 '16 20:02 anuchandra

Is rightBarButtonItem non-nil? Also, you'd want to pass your targetSuperview for the tooltip's hostView, not containerView.

In any case, I've just found that tooltips seem to be positioning themselves incorrectly when using initWithTargetBarButtonItem:, so it may be better to use one of the initWithTargetPoint: methods instead (presumably using your view controller's view as the host view, and manually calculating a point to position it where you want it).

I'll investigate the issue with tooltips positioning incorrectly with initWithTargetBarButtonItem:.

JoeFryer avatar Feb 02 '16 11:02 JoeFryer

For some reason, the rightBarButtonItem is non-nil but targetView, targetSuperview, containerView are nil.

initWithTargetPoint works reasonably OK. The slight issue is that the arrow doesn't quite point to the rightbarbuttonitem. Never the less, its a big step forward. The new app version 1.2 using your tooltips will be in App Store in a few days so you can see them in real use.

https://itunes.apple.com/gb/app/tread-light/id1071002773?mt=8

anuchandra avatar Feb 03 '16 00:02 anuchandra

Strange - perhaps because you're doing it in viewWillAppear:?

Have you tried adjusting the point you supply to get it to point where you want? Or is it as close to the edge of the screen as it'll go?

Cool - let me know when it's live and I'll take a look!

JoeFryer avatar Feb 03 '16 22:02 JoeFryer

I had the same issue with viewDidLoad and viewDidAppear. I'll try adjusting the point for the next app release. For now the new version 1.2 has a slightly mispointed arrow. I'll let you know when 1.2 is in the App Store.

anuchandra avatar Feb 04 '16 17:02 anuchandra

Cool - thanks.

JoeFryer avatar Feb 04 '16 22:02 JoeFryer

Hello, the app and your code is live. Sorry for the delay in getting back to you. https://itunes.apple.com/gb/app/tread-light/id1071002773?mt=8

anuchandra avatar Feb 11 '16 23:02 anuchandra

Oh, no problem at all - I just though it would be interesting to see :)

Thanks for letting me know - I'll take a look

JoeFryer avatar Feb 12 '16 15:02 JoeFryer