将UIKeyboardFrameEndUserInfoKey转换为视图或窗口坐标

对于常量UIKeyboardFrameEndUserInfoKey ,在苹果文档中说:

由于界面方向的改变,这些坐标不考虑应用于窗口内容的任何旋转因子。 因此,在使用它之前,可能需要将矩形转换为窗口坐标(使用convertRect:fromWindow:方法)或查看坐标(使用convertRect:fromView:方法)。

所以如果我使用[view1 convertRect:rect fromView:view2]

我会插入上述参数来让它正确地转换旋转值? 即:

view1 =? rect =? (我假设的键盘框架)view2 =?

一直在尝试一些东西,得到一些有趣的东西。

第一个看法应该是你的看法。 第二个视图应该是零,意思是窗口/屏幕坐标。 从而:

 NSDictionary* d = [notification userInfo]; CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue]; r = [myView convertRect:r fromView:nil]; 

现在你的键盘将占据你的观点。 如果您的视图是当前视图控制器的视图(或其子视图),则旋转等现在被考虑在内。

我试了接受的答案,发现它实际上并没有在视图中提供键盘的CGRect。 我发现我必须将UIScreen对象的CGRect转换为UIWindow对象,并从UIWindow对象转换为UIView对象:

 NSValue * keyboardEndFrame; CGRect screenRect; CGRect windowRect; CGRect viewRect; // determine's keyboard height screenRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; windowRect = [self.view.window convertRect:screenRect fromWindow:nil]; viewRect = [self.view convertRect:windowRect fromView:nil]; 

我使用上面的方法来调整根视图的大小,使其不被键盘隐藏:

 NSTimeInterval duration; CGRect frame; // determine length of animation duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // resize the view frame = self.view.frame; frame.size.height -= viewRect.size.height; // animate view resize with the keyboard movement [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:duration]; self.view.frame = frame; [UIView commitAnimations]; 
 + (void)parseKeyboardNotification:(NSNotification *)notification inRelationToView:(UIView *)view info:(void(^)(NSTimeInterval keyboardAnimationDuration, CGRect keyboardFrameInView, UIViewAnimationOptions keyboardAnimationOptions))callback { NSParameterAssert(notification != nil); NSParameterAssert(view != nil); NSDictionary *userInfo = [notification userInfo]; UIViewAnimationCurve animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; UIViewAnimationOptions animationOption = animationCurve << 16; // https://devforums.apple.com/message/878410#878410 NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // http://stackoverflow.com/a/16615391/202451 CGRect screenRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect windowRect = [view.window convertRect:screenRect fromWindow:nil]; CGRect viewRect = [view convertRect:windowRect fromView:nil]; callback(animationDuration, viewRect, animationOption); } 

可以这样使用

 - (void)keyboardWillShowOrHide:(NSNotification *)notification { [AGKeyboardInfo parseKeyboardNotification:notification inRelationToView:self.view info:^(NSTimeInterval keyboardAnimationDuration, CGRect keyboardFrameInView, UIViewAnimationOptions keyboardAnimationOptions) { [UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationOptions animations:^{ // do any modifications to your views } completion:nil]; }]; }