0

I am making chat application, here how to set label and cell height according to text coming from textfield.

How to set initially cell height = 50, then should change height accordingly text from textfield.

but initially i am getting small cell why?

enter image description here

i have given constraints for label

leading = 100, trailing to imageview = 10, top = 0, bottom = lessthenorequalto 0

for image constraint:

top = 20 height, width = 50 trailing = 20

this is the code:

   override func viewDidLoad() {
    super.viewDidLoad()

   tableView.register(UINib(nibName: "ReceiverChatTableViewCell", bundle: nil), forCellReuseIdentifier: "ReceiverChatTableViewCell")
   tableView.register(UINib(nibName: "ReceiverChatTableViewCell1", bundle: nil), forCellReuseIdentifier: "Cell")

    self.tableView.estimatedRowHeight = 80
    self.tableView.rowHeight = UITableView.automaticDimension
    //tableView.reloadData()

    }
Swift
  • 1,074
  • 12
  • 46
  • same as https://stackoverflow.com/a/32984898/3501225 – Arun Mar 26 '20 at 09:08
  • you can refer https://stackoverflow.com/questions/35986361/swift-tableview-cell-auto-height-with-auto-height-label – dengApro Mar 26 '20 at 09:15
  • Does this answer your question? [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Dilan Mar 26 '20 at 09:59
  • @DilanAnuruddha, i have set `self.tableView.estimatedRowHeight = 80 self.tableView.rowHeight = UITableView.automaticDimension` but for one line text it comes small like above mentioned image.. any idea – Swift Mar 26 '20 at 10:23
  • @DilanAnuruddha, i have deleted heightforrow, edited my question, plese try to help – Swift Mar 26 '20 at 10:28

1 Answers1

2

when you using UITableView.automaticDimension, you need to set top and bottom constraints of your label. If you are set it with using equalTo every time cell keep that value as fixed.when your label has single line label height+(top,bottom padding) is less than your image height.thats why image crops in your case.

using equalTo

Use lessThanOrEqualTo instead of using equalTo for your label bottom constraints.

addSubview(lblMessage)
addSubview(img)

img.translatesAutoresizingMaskIntoConstraints = false
img.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
img.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true
img.widthAnchor.constraint(equalToConstant: 50).isActive = true
img.heightAnchor.constraint(lessThanOrEqualToConstant: 50).isActive = true

lblMessage.translatesAutoresizingMaskIntoConstraints = false
lblMessage.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 16).isActive = true
lblMessage.trailingAnchor.constraint(equalTo: img.leadingAnchor, constant: -16).isActive = true
lblMessage.topAnchor.constraint(equalTo: topAnchor,constant: 16).isActive = true
lblMessage.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -16).isActive = true

enter image description here

Dilan
  • 2,610
  • 7
  • 23
  • 33
  • thank u, it is working for single line text, if i enter long text then cell height is not increasing now – Swift Mar 26 '20 at 11:22
  • one mistake from me. you need to add `lessThanOrEqualToConstant` to imageview height also.i update the code. – Dilan Mar 26 '20 at 11:25
  • i am giving storyboard constraints same like you.. but not getting, can i share my code using github.. its very small project.. u can solve it in minutes.. but i am fresher here – Swift Mar 26 '20 at 12:01
  • thank u, pls once check the code here, you can find in storyboard and xib cell with constraints..https://github.com/SwiftSamples/CellHeightIssue – Swift Mar 26 '20 at 12:29
  • issue is you add label and imageview to another view.so add `lessThanOrEqualToConstant` to these views instead of label and imageview – Dilan Mar 26 '20 at 13:02