Convert UIKeyboardFrameEndUserInfoKey to View or Window Coordinates

For the UIKeyboardFrameEndUserInfoKey constant, Apple docs say:

These coordinates do not take into account the rotation coefficients applied to the contents of the windows as a result of the orientation of the change interface. Thus, you may need to convert the rectangle to a coordinate window (using the convertRect: fromWindow: method) or to view it (using the convertRect: fromView :) method before using it.

So, if I use [view1 convertRect:rect fromView:view2]

What would I invest for the above parameters to get it to correctly convert rotation values? i.e:

view1 =? rect =? (keyboard frame, which I assume) view2 =?

Tried some things and got some funny things.

+25
ios iphone cocoa-touch uikeyboard uiview
Mar 14 '13 at 6:01
source share
3 answers

The first view should be your look. The second view should be zero, that is, the coordinates of the window / screen. In this way:

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

Now you have a rectangle that the keyboard will occupy, in terms of your view. If your view is the current view of the controller of the controller (or its subspecies), rotation, etc. are now taken into account.

+66
Mar 22 '13 at 3:05
source share

I tried the accepted answer and found that it really does not provide a CGRect keyboard in the view. I found that I need to convert a CGRect from a UIScreen to a UIWindow and from a UIWindow to a UIView:

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

I use the above to resize the root view so that it is not hidden by the keyboard:

 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]; 
+21
May 17 '13 at 18:10
source share
 + (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); } 

Can be used as

 - (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]; }]; } 
+1
Jan 22 '14 at
source share



All Articles