iOS 8 通知的 UIKeyboard 建议高度?

UIKeyboard Suggestions height for iOS 8 notification?

我试图在 iOS 8 的键盘顶部保留一个文本字段。但是当用户向上或向下滑动键盘顶部以显示或关闭 iOS 8 字时建议,我需要通知键盘的新高度,以便我可以将我的文本字段向上或向下移动该高度。

我怎样才能做到这一点?

谢谢!

您可以注册 UIKeyboardDidShowNotification,然后使用 UIKeyboardFrameEndUserInfoKey 从通知中获取键盘框架。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(handleKeyboardDidShowNotification:) 
                                             name:UIKeyboardDidShowNotification 
                                           object:nil];


- (void)handleKeyboardDidShowNotification:(NSNotification *)notification
{
    NSDictionary* info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    // Move your textview into view here
}

即使键盘已经显示并且即将更改大小,也会发送此通知,因此只要您在键盘顶部向上或向下滑动,就会收到此通知。

这是我构建的自定义 inputView 中的一些代码。它甚至可以处理自定义视图的动画曲线,使其与键盘的速度相匹配并随之移动。

当建议为 shown/hidden 时通知将触发。

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil]; }

- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary* info = [notification userInfo];
    NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [info objectForKey: UIKeyboardAnimationCurveUserInfoKey];
    CGSize endKbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    CGRect baseFrame = self.frame;
    baseFrame.origin.y = CGRectGetMaxY(self.frame) - (endKbSize.height - 5);

    [UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
        self.frame = baseFrame;
    } completion:^(BOOL finished) {
        self.frame = baseFrame;
    }]; }

- (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary* info = [notification userInfo];
    NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [info objectForKey: UIKeyboardAnimationCurveUserInfoKey];

    [UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
        self.frame = _originalRect;
    } completion:nil]; 
}

Swift 4 解:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    registerKeyboardNotifications()
}

func registerKeyboardNotifications() {
    NotificationCenter.default.addObserver(self,
                                         selector: #selector(keyboardWillShow(notification:)),
                                         name: NSNotification.Name.UIKeyboardWillShow,
                                         object: nil)
    NotificationCenter.default.addObserver(self,
                                         selector: #selector(keyboardWillHide(notification:)),
                                         name: NSNotification.Name.UIKeyboardWillHide,
                                         object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}

@objc func keyboardWillShow(notification: NSNotification) {
    let userInfo: NSDictionary = notification.userInfo! as NSDictionary
    let keyboardInfo = userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue
    let keyboardSize = keyboardInfo.cgRectValue.size
    let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

@objc func keyboardWillHide(notification: NSNotification) {
    scrollView.contentInset = .zero
    scrollView.scrollIndicatorInsets = .zero
}