Shailesh Shailesh - 1 month ago 10
iOS Question

Get position of UIView in respect to its superview's superview

I have a UIView, in which I have arranged UIButtons. I want to find the positions of those UIButtons.

I am aware that

buttons.frame
will give me the positions, but it will give me positions only with respect to its immediate superview.

Is there is any way we can find the positions of those buttons, withe respect to UIButtons superview's superview ?

For instance, suppose there is UIView named "firstView".

Then, I have another UIView, "secondView". This "SecondView" is subview of "firstView".

Then I have UIButton as subview on the "secondView".

Now, is there any way we can find the position of that UIButton, with respect to "firstView" ?

Any help will be appreciated,

Thanks for your time

Answer

You can use this:

Objective C

CGRect frame = [firstView convertRect:buttons.frame fromView:secondView];

Swift

let frame = firstView.convert(buttons.frame, from:secondView)

Documentation reference:

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html