0

I'm working on an app that will show notifications, allow the user to post comments and edit the notification and see comments on the notification.

To do this I have 3 Prototype cells called "Active", "Control" and "Comment" cells. My first two Active and Control look and work fine with their auto-sizing on all devices. The comment cell, however, shrinks on Plus-sized iOS Devices, not including iPads to an unreadable size.

The comment cell simply has 3 labels of slightly different sizes and colors inside of a Stack View. This is on Swift 4.

The list of devices this happens on: 6+, 6s+, 7+, 8+, X, Xs, Xs Max

https://i.sstatic.net/K80Y9.jpg

The above shows the cell as expected on an iPhone XR and as unexpected on an Xs Max

I've tried editing the compression and hugging rules, tried to use a constraint to force the height (which technically worked however it ruined the look and threw a warning).

 func numberOfSections(in tableView: UITableView) -> Int { if activeEvents.count == 0 { self.tableView.isHidden = true self.activityIndicator.isHidden = false self.activityLabel.isHidden = false self.activityIndicator.startAnimating() } else { self.tableView.isHidden = false self.activityIndicator.isHidden = true self.activityLabel.isHidden = true self.activityIndicator.stopAnimating() } return activeEvents.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if activeEvents[section].expanded { return activeEvents[section].comments.count + 2 } else { return 1 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == 0 { let activeEvent = activeEvents[indexPath.section] let cell = tableView.dequeueReusableCell(withIdentifier: "activeCell") as! ActiveCell cell.selectionStyle = .none cell.setCell(active: activeEvent) return cell } else if indexPath.row == 1 { let activeEvent = activeEvents[indexPath.section] let cell = tableView.dequeueReusableCell(withIdentifier: "controlCell") as! ControlCell cell.setNoti(active: activeEvent) cell.selectionStyle = .none cell.delegate = self cell.vc = self return cell } else { let activeEvent = activeEvents[indexPath.section] let activeComment = activeEvent.comments[indexPath.row - 2] let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell") as! CommentCell cell.setCell(comment: activeComment) return cell } } 
7
  • One thing you can do is add a stackView in your cell, then add your text label inside said stackView, just pin everything to the edges and DON'T set the height constraint in neither of the elements, that should auto resize the cell accordingly, also I don't see it in your code, but I assume you are implementing the tableView.rowHeight = UITableViewAutomaticDimension in your didload() right? Commented Jul 17, 2019 at 15:06
  • stackoverflow.com/questions/44766814/… Commented Jul 17, 2019 at 15:08
  • I actually didn't, however, my 3 labels are in a stack view. I apologize for not adding that in the initial question. Also, the Automatic Dimension is not found on my XCode is this something new to swift 5? I'm currently using swift 4. Apologies again long time lurker first time actually answering a question I will edit the question to reflect this. @SamuelChavez Commented Jul 17, 2019 at 15:09
  • @HunterA. no automatic dimension it's not new to swift 5, you should be able to implement it, just change the "tableView" to the actual name of the tableView in your code and that should work, aside from that you just need to check your constraints the, as I said for automatic dimension to work you need to NOT set height constraints on the cells, here is a link that you'll find helpful raywenderlich.com/8549-self-sizing-table-view-cells Commented Jul 17, 2019 at 15:12
  • I've added tableView.estimatedRowheight as well as tableView.rowHeight = UITableView.automaticDimension and removed the Row Heights from the cells in the storyboard. Commented Jul 17, 2019 at 15:13

1 Answer 1

0

One thing you can do is add a stackView in your cell, then add your text label inside said stackView, just pin everything to the edges and DON'T set the height constraint in neither of the elements, that should auto resize the cell accordingly, also I don't see it in your code, but I assume you are implementing the tableView.rowHeight = UITableViewAutomaticDimension in your viewdidload()

You just need to check your constraints, as I said for automatic dimension to work you need to NOT set height constraints on the cells, here is a link that you'll find helpful raywenderlich.com/8549-self-sizing-table-view-cells

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.