Swift: Continuous resizing of tableViewCell while user types in a UITextView

后端 未结 1 448
余生分开走
余生分开走 2021-01-21 09:19

Introduction

Context:

I\'m creating one of my first apps but I\'ve ran into an issue I cannot figure out.

  • I have tableView w

相关标签:
1条回答
  • 2021-01-21 09:37
    • The constraints that determine the height should be laid out in such a way that the textView is attached directly to the top and bottom of the contentView or to views which are connected to the top and bottom of the contentView, so that autolayout can make out the height by connecting the constraints.
    • Make sure that you do not mention a height for the textView and disable scrolling. Let automatic dimension take care of all that.
    • Now all you need to do is call tableView.beginUpdates() and tableView.endUpdates() on textViewDidChange

    Here is my repo which demonstrates the same.


    OP Edit:

    • You should store the additional height that you add in a variable in the cell class so the cells can reload an appropriate height when the tableVIew is reloaded.

    • You should also change textViewDidChange method

      cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.frame.width, height: textView.frame.height) 
      

      to

      let newFrame = ”originalCellHeight” - ”originalTextViewHeight” + textView.contentSize.height
      
      cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: cell.frame.width, height: newFrame )`
      
    0 讨论(0)
提交回复
热议问题