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 相对于其父视图的父视图的位置


- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- UITextView 内容插图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01