代码如下,其中关键是不要使用contentoffset改变位置,而是直接改变tableview的frame ,这样在刷新时才不会导致位置改变
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// storyboard引入的 tableview
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableview.register(cell.self, forCellReuseIdentifier: "cell")
tableview.delegate = self
tableview.dataSource = self
// 添加键盘通知
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(_ noti : Notification) {
for index in 0..<13 {
let cell = tableview.cellForRow(at: IndexPath(row: index, section: 0)) as! cell
if cell.textView.isFirstResponder {
let beginValue = noti.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
let keyboardTop = UIScreen.main.bounds.size.height - (UIApplication.shared.statusBarFrame.size.height + 44) - (beginValue?.cgRectValue.size.height ?? 0)
let inputBottom = cell.frame.maxY - tableview.contentOffset.y
if (keyboardTop < inputBottom) {
let offsetY = inputBottom - keyboardTop
tableview.frame = CGRect(x: 0, y: -(tableview.contentOffset.y + offsetY + 25), width: tableview.frame.size.width, height: tableview.frame.size.height)
}
break;
}
}
}
@objc func keyboardWillHide(_ noti : Notification) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 13
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! cell
return cell
}
// func scrollViewDidScroll(_ scrollView: UIScrollView) {
// if scrollView.isTracking {
// self.view.endEditing(true)
// }
// }
}
class cell : UITableViewCell, UITextViewDelegate {
var textView = UITextView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .red
// 初始化 textView 并设置基本属性
textView.font = UIFont.systemFont(ofSize: 16)
textView.textColor = .black
textView.backgroundColor = .gray
// 将 textView 添加到 contentView 中
contentView.addSubview(textView)
// 设置 textView 的自动布局
textView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5),
textView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 5),
textView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5),
textView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),
textView.heightAnchor.constraint(greaterThanOrEqualToConstant: 35)
])
textView.delegate = self
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func textViewDidChange(_ textView: UITextView) {
// 计算文本内容需要的高度
let size = CGSize(width: textView.bounds.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
// 更新 textView 的高度约束
if let heightConstraint = textView.constraints.first(where: { $0.firstAttribute == .height }) {
heightConstraint.constant = max(35, estimatedSize.height)
}
// 通知 tableView 更新 cell 高度
if let tableView = self.superview as? UITableView {
tableView.beginUpdates()
tableView.endUpdates()
}
}
}











网友评论