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

发布于 2024-11-28 21:26:23 字数 221 浏览 0 评论 0原文

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

我希望此弹出窗口的位置相对于我的 UIView,因此我需要知道 UIView 在窗口中的相对位置。

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

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.

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.

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

乱了心跳 2024-12-05 21:26:24
extension UIView {
    var globalFrame: CGRect {
        return convert(bounds, to: window)
    }
}
extension UIView {
    var globalFrame: CGRect {
        return convert(bounds, to: window)
    }
}
迷鸟归林 2024-12-05 21:26:24
CGRect myFrame = self.superview.bounds;
CGRect myFrame = self.superview.bounds;
原野 2024-12-05 21:26:23

使用时可以使用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 参数表示使用窗口而不是特定视图。

希望这有帮助,

戴夫

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

// 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.

Hope this helps,

Dave

緦唸λ蓇 2024-12-05 21:26:23

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

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

You can ask your .superview to convert your origin for you for you

CGPoint originGlobal = [myView.superview convertPoint:myView.frame.origin 
                                               toView:nil];
吐个泡泡 2024-12-05 21:26:23

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

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

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

CGPoint windowPoint = [myView convertPoint:myView.bounds.origin toView:myWindow];
ι不睡觉的鱼゛ 2024-12-05 21:26:23

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

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

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

I needed to do this in Xamarin.iOS

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

CGRect globalRect = smallView.ConvertRectToView(smallView.Bounds, rootView);
贪恋 2024-12-05 21:26:23

Swift 5 实用程序

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

对于 UIScrollView 的用法

let positionOnWindow = view.originOnWindow

略有不同

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

Swift 5 utility

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

Usage

let positionOnWindow = view.originOnWindow

For UIScrollView it is slightly different

extension UIScrollView {
    var originOnWindow: CGPoint { return convert(contentOffset, to: nil) }
}
生来就爱笑 2024-12-05 21:26:23

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

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

传入您想要相对于 Window 的框架的视图。

Below Xamarin.iOS Implementation Worked For Me.

    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文