для функции, которую я назвал handleKeyboardWillShow , мое входное текстовое поле перемещается выше, чем ожидалось. Я намерен прикрепить текстовое поле к верхней части клавиатуры. Я также добавил код для создания текстового поля, которое, кажется, вызывает проблему.
Создание переменной textField
lazy var inputTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Enter message..."
tf.translatesAutoresizingMaskIntoConstraints = false
tf.delegate = self
return tf
}()
Мой просмотр загружал функцию
override func viewDidLoad() {
super.viewDidLoad()
collectionView.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
collectionView.alwaysBounceVertical = true
collectionView.register(ChatMessageCell.self, forCellWithReuseIdentifier: cellId)
collectionView.backgroundColor = .white
collectionView.keyboardDismissMode = .interactive
becomeFirstResponder()
setUpInputComponents()
setUpKeyboardObserver()
}
Просмотр исчезнет, где я удаляю наблюдателей
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
//MARK: IMPORTANT FOR SHOWING AND HIDING KEYBOARD TO AVOID MEMORY LEAKS
NotificationCenter.default.removeObserver(self)
}
func setUpKeyboardObserver(){
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func handleKeyboardWillShow(notification:NSNotification){
let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
let safeLayoutGuideBot = UIApplication.shared.keyWindow?.safeAreaInsets.bottom
let height = (keyboardFrame?.height)! - safeLayoutGuideBot!
containerViewBottomAnchor?.constant = -height
UIView.animate(withDuration: keyboardDuration!, animations: {
self.view.layoutIfNeeded()
})
}
@objc func handleKeyboardWillHide(notification:NSNotification){
let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
containerViewBottomAnchor?.constant = 0
UIView.animate(withDuration: keyboardDuration!, animations: {
self.view.layoutIfNeeded()
})
}
Это где макет для рассматриваемого textView и контейнерного представления.
var containerViewBottomAnchor: NSLayoutConstraint?
func setUpInputComponents(){
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = .white
view.addSubview(containerView)
containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
containerViewBottomAnchor = containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
containerViewBottomAnchor?.isActive = true
containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
containerView.addSubview(sendButton)
sendButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
sendButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
sendButton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
containerView.addSubview(inputTextField)
inputTextField.leftAnchor.constraint(equalToSystemSpacingAfter: containerView.leftAnchor, multiplier: 8).isActive = true
inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
containerView.addSubview(seperatorLineView)
seperatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
seperatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
seperatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
seperatorLineView.heightAnchor.constraint(equalToConstant: 0.5).isActive = true
}