ios 获取 UIView 相对于其超级视图的超级视图的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17572304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get position of UIView in respect to its superview's superview
提问by Shailesh
I have a UIView, in which I have arranged UIButtons. I want to find the positions of those UIButtons.
我有一个 UIView,我在其中安排了 UIButton。我想找到那些 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.
我知道这buttons.frame
会给我职位,但它只会给我关于其直接上级的职位。
Is there is any way we can find the positions of those buttons, withe respect to UIButtons superview's superview?
有什么办法可以找到这些按钮的位置,就 UIButtons 超级视图的超级视图而言?
For instance, suppose there is UIView named "firstView".
例如,假设有一个名为“firstView”的 UIView。
Then, I have another UIView, "secondView". This "SecondView" is a subview of "firstView".
然后,我有另一个 UIView,“secondView”。这个“SecondView”是“firstView”的子视图。
Then I have UIButton as a subview on the "secondView".
然后我将 UIButton 作为“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"?
现在,有什么方法可以找到 UIButton 相对于“firstView”的位置?
回答by mprivat
You can use this:
你可以使用这个:
Objective-C
目标-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
https://developer.apple.com/documentation/uikit/uiview/1622498-convert
回答by Vlad
Although not specific to the button in the hierarchy as asked I found this easier to visualize and understand:
虽然不是特定于层次结构中的按钮,但我发现这更容易可视化和理解:
From here: original source
从这里:原始来源
ObjC:
对象:
CGPoint point = [subview1 convertPoint:subview2.frame.origin toView:viewController.view];
Swift:
迅速:
let point = subview1.convert(subview2.frame.origin, to: viewControll.view)
回答by iAj
回答by iRavi iVooda
Frame: (X,Y,width,height).
框架:(X,Y,宽度,高度)。
Hence width and height wont change even wrt the super-super view. You can easily get the X, Y as following.
因此,即使是超级超级视图,宽度和高度也不会改变。您可以轻松获得 X,Y,如下所示。
X = button.frame.origin.x + [button superview].frame.origin.x;
Y = button.frame.origin.y + [button superview].frame.origin.y;
回答by Rexb
If there are multiple views stacked and you don't need (or want) to know any possible views between the views you are interested in, you could do this:
如果堆叠了多个视图并且您不需要(或不想)知道您感兴趣的视图之间的任何可能的视图,您可以这样做:
static func getConvertedPoint(_ targetView: UIView, baseView: UIView)->CGPoint{
var pnt = targetView.frame.origin
if nil == targetView.superview{
return pnt
}
var superView = targetView.superview
while superView != baseView{
pnt = superView!.convert(pnt, to: superView!.superview)
if nil == superView!.superview{
break
}else{
superView = superView!.superview
}
}
return superView!.convert(pnt, to: baseView)
}
where targetView
would be the Button, baseView
would be the ViewController.view
.
What this function is trying to do is the following:
If targetView
has no super view, it's current coordinate is returned.
If targetView's
super view is not the baseView (i.e. there are other views between the button and viewcontroller.view
), a converted coordinate is retrieved and passed to the next superview
.
It continues to do the same through the stack of views moving towards the baseView.
Once it reaches the baseView
, it does one last conversion and returns it.
Note: it doesn't handle a situation where targetView
is positioned under the baseView
.
这里targetView
将是按钮,baseView
将是ViewController.view
。
这个函数试图做的是:
如果targetView
没有超级视图,则返回它的当前坐标。
如果targetView's
超级视图不是 baseView(即按钮和 之间有其他视图viewcontroller.view
),则检索转换后的坐标并将其传递给下一个superview
。
它通过移向 baseView 的视图堆栈继续执行相同的操作。
一旦到达baseView
,它就会进行最后一次转换并返回它。
注:它不处理所处的环境targetView
定位下baseView
。
回答by boro
UIView extension for converting subview's frame (inspired by @Rexb answer).
用于转换子视图框架的 UIView 扩展(灵感来自 @Rexb 答案)。
extension UIView {
// there can be other views between `subview` and `self`
func getConvertedFrame(fromSubview subview: UIView) -> CGRect? {
// check if `subview` is a subview of self
guard subview.isDescendant(of: self) else {
return nil
}
var frame = subview.frame
if subview.superview == nil {
return frame
}
var superview = subview.superview
while superview != self {
frame = superview!.convert(frame, to: superview!.superview)
if superview!.superview == nil {
break
} else {
superview = superview!.superview
}
}
return superview!.convert(frame, to: self)
}
}
// usage:
let frame = firstView.getConvertedFrame(fromSubview: buttons.frame)
回答by said altintop
You can easily get the super-super view as following.
您可以轻松获得如下超级超级视图。
secondView -> [button superview]
firstView -> [[button superview] superview]
secondView -> [按钮
超级视图] firstView -> [[按钮超级视图]超级视图]
Then, You can get the position of the button..
然后,您可以获得按钮的位置..
You can use this:
你可以使用这个:
xPosition = [[button superview] superview].frame.origin.x;
yPosition = [[button superview] superview].frame.origin.y;