Get position of UIView in respect to its superview#39;s superview(获取 UIView 相对于其父视图的父视图的位置)
问题描述
我有一个 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 相对于其父视图的父视图的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取 UIView 相对于其父视图的父视图的位置


基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01