iOS – 在窗口中获取视图的位置?

我有一个UIView,它被点击后显示一个popup窗口。 popup窗口需要被添加到主UIWindow,以确保它在所有其他的顶部。

我想这个popup窗口的位置是相对于我的UIView,所以我需要知道我的UIView在窗口中的相对位置。

问题:当UIView不是直接在UIWindow中(它在我的viewController的视图内)时,如何才能在UIWindow中findUIView的位置?

使用可以使用UIView方法covertRect:toView转换为新的坐标空间。 我做了非常类似的事情:

// Convert the co-ordinates of the view into the window co-ordinate space CGRect newFrame = [self convertRect:self.bounds toView:nil]; // Add this view to the main window [self.window addSubview:self]; self.frame = newFrame; 

在我的例子中,self是一个从它的superView中被删除的视图,并被添加到窗口顶部的窗口中。 toView中的nil参数意味着使用窗口而不是特定的视图。

希望这可以帮助,

戴夫

你可以问你的.superview为你转换你的origin给你

 CGPoint originGlobal = [myView.superview convertPoint:myView.frame.origin toView:nil]; 

UIView的convertPoint:toView:不工作? 所以:

 CGPoint windowPoint = [myView convertPoint:myView.bounds.origin toView:myWindow]; 

我需要在Xamarin.iOS中做到这一点

对于那些寻找解决scheme的人来说,最终为我工作的是:

 CGRect globalRect = smallView.ConvertRectToView(smallView.Bounds, rootView); 
 CGRect myFrame = self.superview.bounds;