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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7028850/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:03:59  来源:igfitidea点击:

iOS - Get location of a view in a window?

iphoneobjective-ciosuiwindow

提问by aryaxt

I have a UIView that displays a popup after it's been clicked. The popup needs to be added to the main UIWindow to make sure that it goes on top of everything else.

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

I want the position of this popup to be relative to my UIView, so I need to know the relative location of my UIView in the window.

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

Question: How can I find the location of a UIView in a UIWindow when the UIView is not directly in the UIWindow (It's inside the view of my viewController)?

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

回答by Magic Bullet Dave

Use can use the UIView method covertRect:toView to convert to the new co-ordinate space. I did something very similar:

使用可以使用 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;

In my example, self is a view that is being removed from its superView and added to the window over the top of where it was. The nil parameter in the toView: means use the window rather than a specific view.

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

Hope this helps,

希望这可以帮助,

Dave

戴夫

回答by Yunus Nedim Mehel

You can ask your .superviewto convert your originfor you for you

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

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

回答by Craig

Does UIView's convertPoint:toView: not work? So:

UIView 的 convertPoint:toView: 不起作用吗?所以:

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

回答by Stoyan Berov

I needed to do this in Xamarin.iOS

我需要在Xamarin.iOS 中执行此操作

For anyone looking for a solution there, what eventually worked for me was:

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

CGRect globalRect = smallView.ConvertRectToView(smallView.Bounds, rootView);

回答by soan saini

Below Xamarin.iOS Implementation Worked For Me.

下面的 Xamarin.iOS 实现对我有用。

    CGRect GetRectAsPerWindow(UIView view)
    {
        var window = UIApplication.SharedApplication.KeyWindow;
        return view.Superview.ConvertRectToView(view.Frame, window);
    }

Pass in the view whose frame you want with respect to Window.

传入你想要的关于 Window 的框架的视图。

回答by WantToKnow

Swift 5 utility

Swift 5 实用程序

extension UIView {
    var originOnWindow: CGPoint { return convert(CGPoint.zero, to: nil) }
}

Usage

用法

let positionOnWindow = view.originOnWindow

For UIScrollViewit is slightly different

对于UIScrollView它略有不同

extension UIScrollView {
    var originOnWindow: CGPoint { return convert(contentOffset, to: nil) }
}

回答by ader

CGRect myFrame = self.superview.bounds;