tags:

views:

21

answers:

1

Scenario: I have a UITabBar on the top of my view controller. There are a few buttons on the tabbar. When the user taps on any of the button I want to display a popover just under the button that has been tapped.

I can display the pop over with no problem but I can't figure out how to detect the location of the button that has been pressed, since the UITabItem doesn't expose a frame structure.

How can I solve my problem?

+1  A: 

You can calculate the frame of the UITabBarItem on your own:

CGFloat tabItemWidth = tabBar.bounds.size.width / [tabBar.items count];
CGRect tabItemFrame = CGRectMake(tabItemWidth * [tabBar.items indexOfObject:tabBar.selectedItem], 0, tabItemWidth, tabBar.bounds.size.height);

(I haven't tried this code, but this or something like it should work).

Or, you should be able to subclass UITabBar and override touchesEnded:withEvent:. In that method, store the location of the touch in an ivar. Then in your UITabBarDelegate's tabBar:didSelectItem: you can use the touch's location.

Hilton Campbell
update based on findings:If the tab bar items have images, you can use the UITabBar's selectedItem property to get the currently selected UITabBarItem, then get the frame of the tab bar item's image. Use the image's frame in a call to presentPopoverFromRect:inView:permittedArrowDirections:animated:
amok
the code provided doesn't compile (2nd line) - invalid operands to binary
amok
Right, I didn't realize selectedItem wasn't the index. I've updated it with something that should work, but you may need to work with it. Looks like you already have a solution though.
Hilton Campbell