gpt4 book ai didi

ios - iPhone X 的 KeyboardWillShowNotification 上的 Textview 移动速度高于预期

转载 作者:行者123 更新时间:2023-11-30 10:23:51 27 4
gpt4 key购买 nike

在我标题为handleKeyboardWillShow的函数上,我的输入文本字段的移动速度高于预期。我的目的是将文本字段固定到键盘 View 的顶部。我还将代码添加到了似乎导致问题的文本字段的创建中。

Here is an image depicting my error

创建textField变量

  lazy var inputTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Enter message..."
tf.translatesAutoresizingMaskIntoConstraints = false
tf.delegate = self
return tf
}()

我的 View 确实加载了功能

    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()

}

View 将消失是我删除观察者的地方

 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 和容器 View 布局的位置。

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
}

最佳答案

看来您可能重复计算了安全区域。您应该能够将键盘的框架转换为 View 的坐标空间并完成。这是我的键盘代码的样子。您的布局会略有不同,因为您使用的是自动布局而不是基于框架的布局。

@objc func keyboardShown(_ notification: Notification) {
let keyboardEndFrameWindow = (notification as NSNotification).userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue
let keyboardTransitionDuration = (notification as NSNotification).userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
let keyboardTransitionAnimationCurve = (notification as NSNotification).userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber

let keyboardEndFrameView = self.view.convert(keyboardEndFrameWindow.cgRectValue, from:nil)
var offsetY = keyboardEndFrameView.minY - self.nameField.frame.maxY;
offsetY = min(offsetY, 0);

UIView.animate(withDuration: TimeInterval(keyboardTransitionDuration.doubleValue),
delay: 0.0,
options: UIView.AnimationOptions(rawValue: keyboardTransitionAnimationCurve.uintValue),
animations: { () -> Void in
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: offsetY)
}, completion: nil)
}

关于ios - iPhone X 的 KeyboardWillShowNotification 上的 Textview 移动速度高于预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60192971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com