将 UIView 原点转换为其窗口坐标系
我想获取scrollView在窗口坐标系中的原点。例如,目前,scollView 原点是 (0,51)。但在窗口坐标系中,它应该是 51 + 44(导航栏高度)+20(状态栏高度) = 115。这意味着在窗口坐标系中,scrollView.frame.origin 应该是 (0,115)。我尝试使用 ConvertPoint: 方法,但有时会得到 (0,51) 或 (0,0) 。请提供解决方案。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您没有在正确的视图之间进行转换。视图的框架设置为其超级视图的坐标,而不是其自己的内部坐标,因此如果您尝试将视图的原点转换为窗口坐标,则需要使用超级视图:
但是,转换更简单从视图本身到窗口的零点。这两段代码是等效的,因此根本不需要使用 origin。
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:
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.
我尝试使用 TechZen 解决方案但无济于事。我没有转换scrollView的原点,而是参考了苹果文档UIWindow类参考,并使用此代码将键盘的endCenter转换为我的视图的坐标系
[self.view ConvertPoint:endCentre fromView:[UIApplication sharedApplication].keyWindow]< /代码>。这段代码有效,因此解决了我的问题。 但是,问题仍然是为什么它没有给出scrollView.origin的正确坐标。所以,基本上我通过转换键盘endcenter而不是scrollView.origin得到了解决方法。哦,我正在做所有这些事情,以便在键盘出现在屏幕上时计算滚动视图的新高度。因此,如果有人有解决此问题的方法或任何更好的解决方案,请告诉我们所有人。
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.请注意,这些功能仅在视图控制器完成子视图布局后才起作用。因此,如果您希望在视图加载时使用 viewDidLayoutSubviews / viewDidAppear,但不能在 viewDidLoad / viewWillAppear 等中使用...
这段代码对我有用:
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...
This code worked for me:
Swift:
在 ViewController 的 viewDidAppear(否则元素尚未分层)中:
Swift:
In viewDidAppear(otherwise elements are not yet layered) of your ViewController: