views:

306

answers:

2

Hi,

Could somebody show me how to get the x and y position of a UIButton in cocoa?

The button moves on the screen when clicked and I want to use it's current position to position another UIBUtton relative to it.

I've tried using

CGRect rect=[myButton imageRectForContentRect];

The compiler tells me that myButton may not respond to imageRectForContentRect.

+1  A: 

Use

myButton.frame

Specifically for position, use myButton.frame.origin.x and myButton.frame.origin.y. That would be the top left corner.

Seva Alekseyev
Sweet! Thanks very much
dubbeat
+1  A: 
    CGPoint position = myButton.frame.origin;

This works for any UIView.

The compiler is right (as it's prone to be): there is no method called imageRectForContentRect. There is, however, imageRectForContentRect: which takes a CGRect as its argument. You don't have to use it to get the position.

Felixyz