Keyboard “WillShow” and “WillHide” vs. Rotation

前端 未结 7 510
青春惊慌失措
青春惊慌失措 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:19

    I met the same problem. iOS gaves me incorrect width/height of the keyboard. I used the following snipped in a keyboardDidShow handler:

    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGSize keyboardSize2 = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    LogDbg(@"keyboard size: frameBegin=%@; frameEnd=%@", NSStringFromCGSize(keyboardSize), NSStringFromCGSize(keyboardSize2));
    

    and for portrait and landscape modes of iPad I got respectively:

    2012-06-14 04:09:49.734 -[LoginViewController keyboardDidShow:] 132 [DBG]:keyboard size: frameBegin={768, 264}; frameEnd={768, 264}
    2012-06-14 04:10:07.971 -[LoginViewController keyboardDidShow:] 132 [DBG]:keyboard size: frameBegin={352, 1024}; frameEnd={352, 1024}
    

    Guessing that the width of the keyboard should be greater then the height (yep, i'm so naive) I made a workaround like following:

    if (keyboardSize.width < keyboardSize.height)
    {
        // NOTE: fixing iOS bug: http://stackoverflow.com/questions/9746417/keyboard-willshow-and-willhide-vs-rotation
        CGFloat height = keyboardSize.height;
        keyboardSize.height = keyboardSize.width;
        keyboardSize.width = height;
    }
    

提交回复
热议问题