我有一个UIView,在其中我安排了UIButtons。我想找出那些UIButtons的位置。
我知道buttons.frame会给我位置,但它只会给我它的直接superview的位置。
有没有办法找到这些按钮的位置,与UIButtons superview的superview有关?
例如,假设有一个名为"firstView“的UIView。
然后,我有了另一个UIView,"secondView“。这个"SecondView“是"firstView”的子视图。
然后我将UIButton作为"secondView“的子视图。
->UIViewController.view
--->FirstView A
------->SecondView B
------------>Button现在,我们有没有办法找到UIButton相对于"firstView“的位置?
发布于 2013-07-10 21:50:44
您可以使用以下命令:
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-convert
发布于 2018-04-15 18:42:21
虽然不是特定于所问的层次结构中的按钮,但我发现这更容易可视化和理解:
从这里:original source

ObjC:
CGPoint point = [subview1 convertPoint:subview2.frame.origin toView:viewController.view];Swift:
let point = subview1.convert(subview2.frame.origin, to: viewControll.view)发布于 2017-08-12 14:43:39
针对Swift 3更新了
if let frame = yourViewName.superview?.convert(yourViewName.frame, to: nil) {
print(frame)
}

https://stackoverflow.com/questions/17572304
复制相似问题