gpt4 book ai didi

swift - TextView自动输入回车键【Mac Catalyst】

转载 作者:行者123 更新时间:2023-12-04 04:12:43 35 4
gpt4 key购买 nike

我正在调整我的 iPad 应用程序以适应 Mac Catalyst,并且在该应用程序中,我在 UITableViewCell 中有一个 UITextView,它有一些奇怪的行为。我在 tableview 单元格中的所有 textViews 都在输入返回键。我只是按下一个 textView,它一直按回车键换行(我没有输入任何内容)。我试过使用不同的键盘,但得到的结果相同。

这不会发生在 iPhone 或 iPad 上。这也不会一直发生,它非常随机。 有人知道如何解决这个问题吗?

这是我的代码:

class TextViewCell: UITableViewCell {

override func awakeFromNib() {
super.awakeFromNib()

textView.delegate = self
textView.isScrollEnabled = false
textView.returnKeyType = .done
}
}


// MARK: - textView functions
extension TextViewCell: UITextViewDelegate {

//grow textView as the user types
func textViewDidChange(_ textView: UITextView) {

let size = textView.bounds.size
let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude))

if size.height != newSize.height {
UIView.setAnimationsEnabled(false)
tableView?.beginUpdates()
tableView?.endUpdates()
UIView.setAnimationsEnabled(true)

if let thisIndexPath = tableView?.indexPath(for: self) {
tableView?.scrollToRow(at: thisIndexPath, at: .bottom, animated: false)
}
}
}
}

有没有其他人遇到过这个问题并且知道如何解决它???

最佳答案

尝试在 GUI 线程之外对 UITableView 的可见元素执行任何操作。由于某种原因,在没有此操作的情况下,Catalyst 会失败。

DispatchQueue.main.async{
// stop animations to prevent boucing
UIView.setAnimationsEnabled(false)

// cycle updates to allow frames to be recalculated
self.tableView.beginUpdates()
self.tableView.endUpdates()

// done with all the updates, reset back to normal
UIView.setAnimationsEnabled(true)
}

关于swift - TextView自动输入回车键【Mac Catalyst】,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61446465/

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