根据键盘是否可见来更改textview的框架

时间:2018-10-06 00:17:10

标签: ios swift xcode

我正在使用观察器来查找键盘是否隐藏。加载视图后,键盘应立即自动显示,这将触发观察者来调整视图。加载视图时,将显示键盘,但是在键盘后仍会隐藏部分textview,一旦您隐藏键盘并再次显示它,它将正确调整textview的大小。

在viewDidLoad中,我添加了观察者

let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardDidHideNotification, object: nil)
    notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardDidHideNotification, object: nil)

我的textview成为viewDidAppear中的第一响应者,例如

override func viewDidAppear(_ animated: Bool) {

    noteTextView.becomeFirstResponder()

}

最后,视图进行了调整

@objc func adjustForKeyboard(notification: Notification) {
    let userInfo = notification.userInfo!

    let keyboardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: noteTextView.window)

    if notification.name == UIResponder.keyboardWillHideNotification {
        noteTextView.contentInset = UIEdgeInsets.zero
    } else {
        noteTextView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }

    noteTextView.scrollIndicatorInsets = noteTextView.contentInset

    let selectedRange = noteTextView.selectedRange
    noteTextView.scrollRangeToVisible(selectedRange)
}

我如何解决此问题,使其在第一次出现时正确显示?使用最新版本的xcode。

隐藏键盘之前

Part of textview is behind keyboard

隐藏键盘后,您可以看到textview隐藏在其后面

Textview in full size after hiding keyboard

再次显示键盘后,调整了textview的大小。

Keyboard is shown 2nd time

2 个答案:

答案 0 :(得分:1)

您复制了keyboardDidHideNotification

notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardDidHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardDidHideNotification, object: nil)

您还需要keyboardWillShowNotification

notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardDidHideNotification, object: nil) 
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)

答案 1 :(得分:0)

//MARK:- View delegates
override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidChange), name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)

}

//TODO: When WE edit the textfields >> this method calls
@objc func keyboardDidChange(notification: Notification) {
    debugPrint(notification)
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue, let keyboardAnimation : Double = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double{
        print("UIKeyboardWillChangeFrame keyboardAnimation : \(keyboardAnimation)ms")
        if self.view.frame.origin.y == 0{
            UIView.animate(withDuration: keyboardAnimation) {
               self.view.frame.origin.y -= keyboardSize.height
            }
        }
        else{
            UIView.animate(withDuration: keyboardAnimation) {
                self.view.frame.origin.y = 0
            }
        }
    }
}

//TODO: Removes the observation on view deinit
deinit{
    NotificationCenter.default.removeObserver(Notification.Name.UIKeyboardWillChangeFrame)
}