Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何获取实际键盘高度(键盘高度减去安全区域插入)_Ios_Swift - Fatal编程技术网

Ios 如何获取实际键盘高度(键盘高度减去安全区域插入)

Ios 如何获取实际键盘高度(键盘高度减去安全区域插入),ios,swift,Ios,Swift,我尝试获取键盘的高度,以便在键盘出现时更新表格内容插图;但是,UIResponder.keyboardWillShowNotification的通知显示了具有安全区域高度的框架。有没有办法得到实际的键盘框架 键盘高度的代码: if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue { let keyboardRectangle =

我尝试获取键盘的高度,以便在键盘出现时更新表格内容插图;但是,
UIResponder.keyboardWillShowNotification的通知显示了具有安全区域高度的框架。有没有办法得到实际的键盘框架

键盘高度的代码:

if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height

    print("show: \(keyboardHeight)")
    tableView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 18 + keyboardHeight, right: 0)
    tableView.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 18 + keyboardHeight, right: 0)
}
但这给出了以下键盘的高度:

我想要的是:

首先,您必须获得安全区域底部高度,然后 将其从键盘总高度中排除。这样你只会得到 键盘高度,无底部安全区域

比苏多代码:

让keyboardHeight=键盘高度-底部填充


希望它能对您有所帮助。

在计算约束值时,尝试减去安全区域底部插图的高度

下面是一个处理UIKeyboardWillChangeFrame通知的示例实现

@objc private func keyboardWillChange(_ notification: Notification) {
    guard let userInfo = (notification as Notification).userInfo, let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
    let newHeight: CGFloat
    if #available(iOS 11.0, *) {
        newHeight = value.cgRectValue.height - view.safeAreaInsets.bottom
    } else {
        newHeight = value.cgRectValue.height
    }
    myConstraint.value = newHeight
}
@objc private func keyboardWillChange(_ notification: Notification) {
    guard let userInfo = (notification as Notification).userInfo, let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
    let newHeight: CGFloat
    if #available(iOS 11.0, *) {
        newHeight = value.cgRectValue.height - view.safeAreaInsets.bottom
    } else {
        newHeight = value.cgRectValue.height
    }
    myConstraint.value = newHeight
}