Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView automatically typing return key [Mac Catalyst]

I'm adapting my iPad app to Mac Catalyst and in the app I have a UITextView inside a UITableViewCell with some strange behavior. All of my textViews inside tableview cells are entering the return key. I just press on a textView and it's stuck pressing the return key making new lines (I'm not typing anything). I've tried using different keyboards and I'm getting the same outcome.

This doesn't happen on iPhone or iPad. This also doesn't happen all the time it's very random. Does anyone know how to fix this?

Here's my code:

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

Has anyone else run into this issue and knows how to fix it???

like image 526
ap123 Avatar asked May 10 '26 09:05

ap123


1 Answers

Try performing any operation on the visible elements of the UITableView off the GUI thread. For some reason on Catalyst without this operations fail.

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)
}
like image 152
Christopher Brown Avatar answered May 12 '26 07:05

Christopher Brown