iOS - Get location of a view in a window?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 19:09:58

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

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

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

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

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

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

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.

CGRect myFrame = self.superview.bounds;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!