33

是否可以动态获取键盘的框架,实际上是它的高度?因为我有一个UITextView,我想根据键盘框架的高度来调整它的高度,当键盘的输入法改变时。如您所知,不同的输入法可能有不同的键盘框架高度。

4

4 回答 4

102

试试这个:

[[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..........
}

教程了解更多信息

于 2012-08-17T03:57:37.313 回答
7

只需按照 Apple 的本教程进行操作,您就会得到您想要的。苹果文档。为了确定键盘覆盖的区域,请参考本教程

于 2012-08-16T14:29:23.710 回答
2

对于 Swift 3 用户,@Hector 代码(有一些补充)将是:

在您viewDidLoad添加观察者:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(_:)), name: .UIKeyboardDidShow , object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidHide(_:)), name: .UIKeyboardDidHide , object: nil)

然后实现这些方法:

func keyboardDidShow(_ notification: NSNotification) {
     print("Keyboard will show!")
     // print(notification.userInfo)

     let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size
     print("Keyboard size: \(keyboardSize)")  

     let height = min(keyboardSize.height, keyboardSize.width)
     let width = max(keyboardSize.height, keyboardSize.width)

}

func keyboardDidHide(_ notification: NSNotification) {
        print("Keyboard will hide!")
}
于 2017-01-08T09:59:29.610 回答
0

您可以将此代码添加到包含 Swift 3 中的文本字段的视图中。这将使文本字段使用键盘上下动画。

private var keyboardIsVisible = false
private var keyboardHeight: CGFloat = 0.0

// MARK: Notifications

private func registerForKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
private func deregisterFromKeyboardNotifications() {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

// MARK: Triggered Functions

@objc private func keyboardWillShow(notification: NSNotification) {
    keyboardIsVisible = true
    guard let userInfo = notification.userInfo else {
        return
    }
    if let keyboardHeight = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height {
        self.keyboardHeight = keyboardHeight
    }
    if !textField.isHidden {
        if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber,
            let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
            animateHUDWith(duration: duration.doubleValue,
                           curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut,
                           toLocation: calculateTextFieldCenter())
        }
    }
}

@objc private func keyboardWillBeHidden(notification: NSNotification) {
    keyboardIsVisible = false
    if !self.isHidden {
        guard let userInfo = notification.userInfo else {
            return
        }
        if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber,
            let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
            animateHUDWith(duration: duration.doubleValue,
                           curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut,
                           toLocation: calculateTextFieldCenter())
        }
    }
}

// MARK: - Helpers

private func animateHUDWith(duration: Double, curve: UIViewAnimationCurve, toLocation location: CGPoint) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(TimeInterval(duration))
    UIView.setAnimationCurve(curve)
    textField.center = location
    UIView.commitAnimations()
}

private func calculateTextFieldCenter() -> CGPoint {
    if !keyboardIsVisible {
        return self.center
    } else {
        let yLocation = (self.view.frame.height - keyboardHeight) / 2
        return CGPoint(x: self.center.x, y: yLocation)
    }
}
于 2017-06-30T17:47:01.783 回答