iOS 8 auto-sizing UITableViewCell with UITextView

前端 未结 6 1523
余生分开走
余生分开走 2021-02-12 12:11

iOS 8 introduced a way for tableViews to automatically adjust their cell\'s height based on their content (via AutoLayout).

// in viewDidLoad:
tableView.rowHeigh         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-12 12:52

    Solution for Swift 4

    func textViewDidChange(_ textView: UITextView) {
        let fixedWidth = textView.frame.size.width
        let oldSize = textView.frame.size
        let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))
        if oldSize.height != newSize.height {
            UIView.setAnimationsEnabled(false)
            var newFrame = textView.frame
            newFrame.size = CGSize(width: fmax(newSize.width, fixedWidth), height: newSize.height)
            textView.frame = newFrame
            self.tableView.beginUpdates()
            self.tableView.endUpdates()
            UIView.setAnimationsEnabled(true)
        }
    }
    

提交回复
热议问题