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

ary*_*axt 33 iphone objective-c uiwindow ios

我有一个UIView,它在点击后显示一个弹出窗口.弹出窗口需要添加到主UIWindow以确保它在其他所有内容之上.

我希望这个弹出窗口的位置与我的UIView相关,所以我需要知道我的UIView在窗口中的相对位置.

问题:当UIView不是直接在UIWindow中时(如果在我的viewController视图中),如何在UIWindow中找到UIView的位置?

Mag*_*ave 65

使用可以使用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;
Run Code Online (Sandbox Code Playgroud)

在我的示例中,self是一个从superView中删除并添加到窗口顶部的视图.toView中的nil参数:表示使用窗口而不是特定视图.

希望这可以帮助,

戴夫


Yun*_*hel 12

您可以要求您为您.superview转换origin

CGPoint originGlobal = [myView.superview convertPoint:myView.frame.origin 
                                               toView:nil];
Run Code Online (Sandbox Code Playgroud)


Cra*_*aig 8

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

CGPoint windowPoint = [myView convertPoint:myView.bounds.origin toView:myWindow];
Run Code Online (Sandbox Code Playgroud)


Sto*_*rov 6

我需要在Xamarin.iOS中这样做

对于那里寻找解决方案的人来说,最终对我有用的是:

CGRect globalRect = smallView.ConvertRectToView(smallView.Bounds, rootView);
Run Code Online (Sandbox Code Playgroud)