Take the 2-minute tour ×

So I have a large UIButton, it is a UIButtonTypeCustom, and the button target is called for UIControlEventTouchUpInside. My question is how can I determine where in the UIButton the touch occured. I want this info so I can display a popup from the touch location. Here is what I've tried:

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

and various other iterations. The button's target method get info from it via the sender:

    UIButton *button = sender;

So is there any way I could use something like: button.touchUpLocation?

I looked online and couldn't find anything similar to this so thanks in advance.

share|improve this question
There is no touchUpLocation, UIButton is a control you can not get the location of UIButton click. developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/… –  Praveen-K Sep 10 '11 at 22:03
I didn't mean literally touchUpLocation, I meant something like that –  Andrew Sep 10 '11 at 22:09
@Praveen-K, that's incorrect. UIKit supports actions that take a second parameter, the UIEvent which triggered the action. From that you can get the list of touches, and each touch has a location. See the code I posted below. It's true that UIButton doesn't have a method that returns the location of the last click, however. –  Caleb Sep 11 '11 at 1:40
add comment

2 Answers

up vote 29 down vote accepted
UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];

That's the right idea, except that this code is probably inside an action in your view controller, right? If so, then self refers to the view controller and not the button. You should be passing a pointer to the button into -locationInView:.

Here's a tested action that you can try in your view controller:

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
    UIView *button = (UIView *)sender;
    UITouch *touch = [[event touchesForView:button] anyObject];
    CGPoint location = [touch locationInView:button];
    NSLog(@"Location in button: %f, %f", location.x, location.y);
share|improve this answer
Thanks that completely solved my problem. I thought it couldn't be done, so this is great. –  Andrew Sep 11 '11 at 3:13
What a great snippet of code - sometimes these things show up which reaffirm your belief in human nature :-) –  SomaMan Mar 2 '12 at 9:44
Is there a way to get the event from - (IBAction) buttonPressed: (id) sender ...? (That is, I have a huge project and I'd like to bottleneck this, without changing all the buttons to ...forEvent:, if I can.) Thanks! –  Olie Nov 7 '13 at 22:36
@Olie As far as I know, if you want the event you'll need to change the signature of the action to include the event parameter. –  Caleb Nov 8 '13 at 3:00
but if you got multiple touches, this doesn't get you any further –  Peter Lapisu Dec 11 '13 at 16:06
show 1 more comment

Did you try to add a gesture recognizer to your button?

share|improve this answer
I didn’t try that because in my past experiences with gesture recognizers, they don't work with subviews. I'm sure I was doing something wrong though. But it didn't seem like a gesture situation. Iwanted to know when the button was pushed, not like when a view was tapped. But thanks for your answer –  Andrew Sep 11 '11 at 3:12
add comment

Your Answer


