Показать UIView над клавиатурой не работает должным образом - PullRequest
0 голосов
/ 07 октября 2018

Я пытаюсь переместить constraintToBottom.constant из UIView, где мой UITextField находится над клавиатурой, как только он отображается.Я установил наблюдателя Notification, но по какой-то причине он не работает.

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        UIView.animate(withDuration: 0.3, animations: {
            self.constraintToBottom.constant = keyboardSize.height
            print("constant", self.constraintToBottom.constant)
        })
    }
}

Когда я печатаю constraintToBottom.constant, он показывает:

константа 346.0

И все же UIView все еще не виденвыше клавиатуры как обычно.Что я делаю не так?

ОБНОВЛЕНИЕ

Как и предполагалось, минус делает трюк, НО также есть дополнительное пространство, и это выглядит так:

enter image description here

1 Ответ

0 голосов
/ 07 октября 2018

Вам нужно изменить макет представления

self.constraintToBottom.constant = -1 * keyboardSize.height
UIView.animate(withDuration: 0.3, animations: {
     self.view.layoutIfNeeded()
     print("constant", self.constraintToBottom.constant)
})
...