Getting or setting the hit area of UIButtonTypeInf

2019-03-31 20:24发布

问题:

So it seems that the tappable area of an UIButton with the type of UIButtonTypeInfoLight is much larger than its frame. Because of this, I can not put a button right next to it, trying to tap it will tap the info button instead.

_infoButton.frame = CGRectMake(0, 0, 40, height);
_searchButton.frame = CGRectMake(CGRectGetMaxX(_infoButton.frame), 0, 40, height);

Does anyone have any insite on how to get the tappable area of the button?

Edit:having the button next the info button it is not my core question (just an illustration of why it might be important). My question is about finding out the "hit area" for a button of these types.

回答1:

I'm not sure about looking up the specific rect of the hit area from code, but there is a way that you can specifically set the area that will be tappable. You can subclass your UIButton and override UIView's hitTest:withEvent: to set the area. Here's an example that shows expanding the hit area, but I'm pretty sure you could use it to decrease the area as well: Larger Hit Area for UIButton

I suppose you could use trial and error with this method to tap areas and find out what is or isn't within the hit area's bounds. I know it's not ideal, but that's what I've found.