如何获取UI按钮iPhone的X和Y位置

dub*_*eat 8 iphone objective-c uibutton

有人可以告诉我如何在可可中获得UIButton的x和y位置吗?

单击时按钮在屏幕上移动,我想使用它的当前位置来定位相对于它的另一个UIBUtton.

我试过用了

CGRect rect=[myButton imageRectForContentRect];
Run Code Online (Sandbox Code Playgroud)

编译器告诉我myButton可能不响应imageRectForContentRect.

Sev*_*yev 17

使用

myButton.frame
Run Code Online (Sandbox Code Playgroud)

特别是对于位置,使用myButton.frame.origin.x和myButton.frame.origin.y.那将是左上角.


Fel*_*xyz 5

    CGPoint position = myButton.frame.origin;
Run Code Online (Sandbox Code Playgroud)

这适用于任何UIView.

编译器是正确的(因为它很容易):没有调用的方法imageRectForContentRect.然而,imageRectForContentRect:有一个以CGRect为参数.你不必用它来获得这个位置.