问题描述
我有一个 UIView,我在其中安排了 UIButton.我想找到那些 UIButtons 的位置.
I have a UIView, in which I have arranged UIButtons. I want to find the positions of those UIButtons.
我知道 buttons.frame
会给我职位,但它只会给我与其直接超级视图相关的职位.
I am aware that buttons.frame
will give me the positions, but it will give me positions only with respect to its immediate superview.
关于 UIButtons 超级视图的超级视图,我们有什么方法可以找到这些按钮的位置?
Is there is any way we can find the positions of those buttons, withe respect to UIButtons superview's superview?
例如,假设有一个名为firstView"的 UIView.
For instance, suppose there is UIView named "firstView".
然后,我有另一个 UIView,secondView".这个SecondView"是firstView"的子视图.
Then, I have another UIView, "secondView". This "SecondView" is a subview of "firstView".
然后我将 UIButton 作为secondView"上的子视图.
Then I have UIButton as a subview on the "secondView".
->UIViewController.view
--->FirstView A
------->SecondView B
------------>Button
现在,我们有什么方法可以找到那个 UIButton 相对于firstView"的位置?
Now, is there any way we can find the position of that UIButton, with respect to "firstView"?
推荐答案
你可以这样用:
Objective-C
CGRect frame = [firstView convertRect:buttons.frame fromView:secondView];
斯威夫特
let frame = firstView.convert(buttons.frame, from:secondView)
文档参考:
https://developer.apple.com/documentation/uikit/uiview/1622498-转换
这篇关于获取 UIView 相对于其父视图的父视图的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!