IOS键盘建议栏在内容之上

时间:2018-04-21 01:06:09

标签: ios swift xcode user-interface

对于我的RMIT课程,我正在使用Swift'应用程序开发。预订,现在正在处理滚动视图。

我实现了本书告诉我的代码,使滚动视图不受键盘影响,但它没有注意到键盘顶部的建议栏,所以this happens是绝对不理想,因为它是我尝试输入文本的电话号码字段。

我使用的代码如下

    func registerForKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)),name: .UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil)
}

@objc func keyboardWasShown(_ notification: NSNotification) {
    guard let info = notification.userInfo,
        let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue
        else {return}
    let keyboardFrame = keyboardFrameValue.cgRectValue
    let keyboardSize = keyboardFrame.size

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

@objc func keyboardWillBeHidden(_ notification: NSNotification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

我想知道这段代码是否是在建议栏存在之前编写的,以及是否有新方法可以包含建议栏。

1 个答案:

答案 0 :(得分:0)

将此密钥UIKeyboardFrameBeginUserInfoKey替换为UIKeyboardFrameEndUserInfoKey