Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 a subview of "firstView".

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

->UIViewController.view
--->FirstView A
------->SecondView B
------------>Button

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
493 views
Welcome To Ask or Share your Answers For Others

1 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:

https://developer.apple.com/documentation/uikit/uiview/1622498-convert


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...