How to get the X and Y position of a UI button iPhone

杀马特。学长 韩版系。学妹 提交于 2020-01-01 09:27:44

问题


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:


Use

myButton.frame

Specifically for position, use myButton.frame.origin.x and myButton.frame.origin.y. That would be the top left corner. This is the easiest solution for finding a button's position.




回答2:


    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.



来源:https://stackoverflow.com/questions/2319642/how-to-get-the-x-and-y-position-of-a-ui-button-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!