ios 将 UIView 原点转换为其窗口坐标系

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

Convert a UIView origin point to its Window coordinate system

iosuiviewcoordinate-systemsuiwindow

提问by Nishit

I want to get the origin of scrollView in the window coordinate system. For Example, presently, scollView origin is (0,51). But in window coordinate system it should be 51 + 44(navigation bar height)+20(status bar height) = 115. Means in window coordinate system the scrollView.frame.origin should be (0,115). I tried with convertPoint: methods but getting (0,51) or (0,0) sometimes. Please provide a solution to this. Thanks

我想在窗口坐标系中获取 scrollView 的原点。例如,目前,scollView 原点是 (0,51)。但是在窗口坐标系中它应该是 51 + 44(导航栏高度)+20(状态栏高度)= 115。意味着在窗口坐标系中 scrollView.frame.origin 应该是(0,115)。我尝试使用 convertPoint: 方法,但有时会得到 (0,51) 或 (0,0)。请提供解决方案。谢谢

采纳答案by Nishit

I tried with TechZen solution but to no avail. Instead of converting scrollView's origin,I referred to apple docs UIWindow class Reference and converted the endCenter of the keyboard to my view's coordinate system by using this code [self.view convertPoint:endCentre fromView:[UIApplication sharedApplication].keyWindow]. This piece of code worked and hence solved my problem. However, the question still remains as to why it does not gives the proper coordinate of scrollView.origin.So, basically I got a workaround by converting keyboard endcenter instead of scrollView.origin. Oh, and I was doing all this stuff in order to calculate my scrollView's new height when keyboard appears on screen. So, if somebody has a solution to this problem or any better solution, Please let all of us know.

我尝试使用 TechZen 解决方案,但无济于事。我没有转换 scrollView 的原点,而是参考了苹果文档 UIWindow 类 Reference 并使用此代码将键盘的 endCenter 转换为我的视图坐标系[self.view convertPoint:endCentre fromView:[UIApplication sharedApplication].keyWindow]。这段代码有效,因此解决了我的问题。但是,问题仍然存在,为什么它没有给出 scrollView.origin 的正确坐标。所以,基本上我通过转换键盘 endcenter 而不是 scrollView.origin 得到了一个解决方法。哦,我正在做所有这些事情,以便在键盘出现在屏幕上时计算我的滚动视图的新高度。因此,如果有人对此问题有解决方案或任何更好的解决方案,请告诉我们所有人。

回答by TechZen

It sounds like your not converting between the proper views. A view's frame is set to the coordinates of it's superview, not its own internal coordinates, so if you were trying to convert the origin of a view to window coordinates, you would need to use the superview:

听起来您没有在正确的视图之间进行转换。视图的框架设置为其父视图的坐标,而不是其自身的内部坐标,因此如果您尝试将视图的原点转换为窗口坐标,则需要使用超级视图:

[[self superview] convertPoint:self.frame.origin toView:theWindow];

However, it is even simpler to convert the zero point from the view itself to the window. The two pieces of code are equivalent, and so it isn't necessary to use the origin at all.

但是,将零点从视图本身转换为窗口更简单。的两段代码是等效的,所以它是没有必要使用原点的。

[self convertPoint:CGPointZero toView:theWindow];

回答by david72

Note that these functions will only work after view controller finished laying out subviews. So if you want that when view loads, you can use viewDidLayoutSubviews / viewDidAppear, but not in viewDidLoad / viewWillAppear etc...

请注意,这些功能仅在视图控制器完成子视图布局后才能工作。因此,如果您希望在视图加载时使用 viewDidLayoutSubviews / viewDidAppear,但不能在 viewDidLoad / viewWillAppear 等中使用...

This code worked for me:

这段代码对我有用:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    CGPoint originInSuperview = [self.view.superview convertPoint:CGPointZero fromView:self.scrollView];
}

回答by Juan Boero

Swift:

迅速:

In viewDidAppear(otherwise elements are not yet layered) of your ViewController:

在 ViewController 的 viewDidAppear(否则元素尚未分层)中:

        var yourFrame = yourScrollView.convertRect(yourScrollView.frame, to: self.view)

        //PRINT THE WHOLE RECT
        print(yourFrame)

        //PRINT A DETAILED VERSION OF THE FRAME, ALSO CAN PERFORM CALCULATION BETWEEN THE RECT PARAMETERS TO GET , FOR EXAMPLE, WIDTH POINT IN THE SUPERVIEW:
        print("X: \(yourFrame.origin.x) Y: \(yourFrame.origin.y) WIDTH: \(yourFrame.width) HEIGHT: \(yourFrame.height)")