I have a custom button in another view directly above my UITabBar. It seems like there's a hidden "hit area" above the UITTabBar that is preventing me from hitting the bottom half of my custom button in another view. The button subview is on top of all other views including the custom UITabBar.
It's really easy to notice this effect in th开发者_运维问答e simulator using the UICatalog sample code. Head to the toolbar section. Position your mouse cursor about 5-10 pixels above the tool bar items on the bottom and click to see that you can trigger the touch event way above the button.
I need to figure out how to restrict this hit area to the bounds of the uitoolbar or uitabbar itself and not let the iPhone do any sort of hit accessibility magic.
I think I've exhausted all options :\ I thought clipsToBounds (on the UITabBar) would do the trick, but apparently not.
Also I'm doing this completely in code, so no Interface Builder...
For anyone who stumbles across this question, I have a solution for this, although it falls well into "dirty hack" territory, and may break with newer (than 3.0) iPhone OS releases... use at your peril!
I created a UITabBar category with a hitTest:withEvent:
implementation, and noticed when I set a breakpoint in the body of this method that the x,y values in the supplied CGPoint struct were rather odd - when touching in the ~15px or so ABOVE the tab bar the values look something like (x=23.482749, y=12.938475), whereas if you touch within the tab bar the values were nicely rounded to the nearest integer, e.g. (x=23, y=12).
Now, for some reason I was expecting the touches in the ~15px "hit zone" above the tab bar to be negative for the y axis (which is why I started down this path in the first place), but alas, no. You can touch in two different places and get similar coordinates, such as in the example above.
So, my "solution": I'm relying on a touch in the tab bar itself always having an integer-rounded value for the y axis, otherwise I'm assuming it's in the overlap zone above the tab bar. I tested this for quite a while, and it seems to consistently behave this way.
The code:
@implementation UITabBar (Me)
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if ([[NSString stringWithFormat:@"%.6f", point.y] hasSuffix:@".000000"])
{
return [super hitTest:point withEvent:event];
}
else
{
return nil;
}
}
@end
精彩评论