Keyboard “WillShow” and “WillHide” vs. Rotation

前端 未结 7 513
青春惊慌失措
青春惊慌失措 2021-01-31 04:56

I\'ve got a view controller listening for both UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. The handlers for these notifications adjust various parts of th

相关标签:
7条回答
  • 2021-01-31 05:26

    Maybe a bit late, but I've just run into the same issue and have a nice solution for it that avoids any kind of work arounds (unless of course apple change things)

    Basically, when the notification center calls your method for UIKeyboardWillShowNotification (or any of the other notifications), the frame that it gives you for UIKeyboardFrameBeginUserInfoKey is in context of the window, NOT your view. The problem with this, is that the windows coordinate system is always in portrait, regardless of the devices orientation, hence you're finding the width and height the wrong way round.

    If you want to avoid your work around, simply convert the rectangle into the coordinate system of your view (which does change according to the orientation). To do this, do something like the following :

    - (void) keyboardWillShow:(NSNotification *)aNotification
    {
         CGRect keyboardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        CGRect convertedFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
    
        ......
        /* Do whatever you want now with the new frame.
         * The width and height will actually be correct now
         */
        ......
    }
    

    Hopefully this should be what you're after :)

    0 讨论(0)
提交回复
热议问题