将UIKeyboardFrameEndUserInfoKey转换为视图或窗口坐标 [英] Convert UIKeyboardFrameEndUserInfoKey to View or Window Coordinates
问题描述
对于常量 UIKeyboardFrameEndUserInfoKey
,在Apple文档中说:
For the constant UIKeyboardFrameEndUserInfoKey
, in the Apple docs it says:
这些坐标不考虑由于界面定向
改变而应用于窗口内容的任何旋转因子
。因此,在使用它之前,您可能需要将矩形转换为窗口
坐标(使用convertRect:fromWindow:方法)或查看
坐标(使用convertRect:fromView:方法)。
These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it.
所以如果我使用 [view1 convertRect:rect fromView:view2]
我将为上述参数插入什么,以使它正确地转换旋转值?即:
What would I insert for the above parameters to get it to convert the rotation values correctly? ie:
view1 =?
rect =? (我假设的键盘框架)
view2 =?
view1 = ? rect = ? (the keyboard frame I'm assuming) view2 = ?
尝试一些东西,得到一些有趣的东西。
Been trying some things and getting some funny stuff.
推荐答案
第一个视图应该是你的视图。第二个视图应该是nil,意味着窗口/屏幕坐标。因此:
The first view should be your view. The second view should be nil, meaning window/screen coordinates. Thus:
NSDictionary* d = [notification userInfo];
CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
r = [myView convertRect:r fromView:nil];
现在你的键盘会占据你的视图。如果您的视图是当前视图控制器的视图(或其子视图),则旋转等等现在被考虑。
Now you have the rect that the keyboard will occupy, in terms of your view. If your view is the current view controller's view (or a subview thereof), rotation and so forth are now accounted for.
这篇关于将UIKeyboardFrameEndUserInfoKey转换为视图或窗口坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!