How to get UIKeyboard size with iOS

前端 未结 4 1710
后悔当初
后悔当初 2020-11-27 12:35

Is there some way to get UIKeyboard size programmatically. 216.0f height and 162.0f height in landscape.

Following seem to be deprecated. Is there some way that work

4条回答
  •  北海茫月
    2020-11-27 12:42

    You should use the UIKeyboardWillChangeFrameNotification instead, because some international keyboards, like the Chinese keyboard, will change frames during use. Also make sure to convert the CGRect into the proper view, for landscape use.

    //some method like viewDidLoad, where you set up your observer.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    - (void)keyboardWillChange:(NSNotification *)notification {
        CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; //this is it!
    }
    

提交回复
热议问题