Get the frame of the keyboard dynamically

前端 未结 4 1169
逝去的感伤
逝去的感伤 2020-12-14 00:48

Is it possible to get the frame, actually its height, of the keyboard dynamically? As I have a UITextView and I would like to adjust its height according to the

4条回答
  •  旧时难觅i
    2020-12-14 01:16

    try this:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];
    
    - (void)keyboardWasShown:(NSNotification *)notification
    {
    
    // Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    //Given size may not account for screen rotation
    int height = MIN(keyboardSize.height,keyboardSize.width);
    int width = MAX(keyboardSize.height,keyboardSize.width);
    
    //your other code here..........
    }
    

    Tutorial for more information

提交回复
热议问题