实现UITableView与键盘弹出同步滚动的问题求助
UITableView与键盘同步滚动延迟问题,附SwiftUI解决方案需求
我有一个包含4个单元格的UITableView,每个单元格内嵌入了一个带有长文本的UITextView。当用户开始编辑文本时,键盘弹出后UITableView会向上偏移,但存在轻微延迟,我希望实现像苹果笔记App那样,和键盘弹出完全同步的滚动效果。
实际效果:
期望效果:
以下是我当前使用的代码:
private func addKeyboardObservers() { 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 private func keyboardWillShow(notification: Notification) { guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0) } @objc private func keyboardWillHide() { tableView.contentInset = .zero }
另外,我最初尝试用SwiftUI实现该功能,但滚动表现更加混乱,如果有SwiftUI环境下的解决方案,我会非常感激。
内容的提问来源于stack exchange,提问作者Shalugin
相关产品推荐
相关产品推荐

