1

I've successfully created UITableView with custom cells (automatically sized) that contain a label and UITextView - detailsTextView.

When scrolling of detailsTextView is disabled, the cells are properly resized according to textView's text. I don't want, however, to have extremely large cells and set maximum height of a cell (and enable scrolling for detailsTextView when it reaches max cell height).

How can I achieve this? When enabling scrolling for detailsTextView autoresizing of cells shrinks it to 0 height (overriden by min. height constraint in IB), but still it does not fill my planned maximum size of cell.

override func viewDidLoad() {
   mainTableView.rowHeight = UITableViewAutomaticDimension
   mainTableView.estimatedRowHeight = 200
   self.mainTableView.register(UINib.init(nibName: EventCell.nibName,
                                           bundle: Bundle.main),
                                forCellReuseIdentifier: EventCell.cellIdentifier)
   ...
}
izik461
  • 1,131
  • 12
  • 32

3 Answers3

1

The idea is simple. You need to keep track of UITextView's height change in textViewDidChange method.

Then if UITextView exceeds your maximum predefined height then you are gonna add a constraint which restrict the UITextView's height growth. Constraint should be something like setting minimum height of UITextView to your maximum predefined height and enable the scroll for UITextView.

Jaffer Sheriff
  • 1,444
  • 13
  • 33
  • Thanks - this is actually answer to my question, but according to other suggestsions it's a bad practice from UX perspective. I'll try something else. Thanks! – izik461 Feb 04 '17 at 19:41
1

If you use only auto layout, UITextView can define it's own size only if scrolling is disabled, unfortunately.

You should try to avoid recursive layout passes (e.g. when text view's height changes, then install more constraints, this will cause the height to change again, etc, etc).

What you can do is limit maximumNumberOfLines of your textView, and provide other ways to see the full text: as it was pointed out, it is a very bad UX practice to have a scroll view within another scroll view.

hybridcattt
  • 3,001
  • 20
  • 37
0

From a UI/UX standpoint I wouldn't do that what you are trying to do. Having a Scrollview (UITableview) with another scrollview in it (UITextview) is bad. It can confuse the user because he could scroll the "textbox" on smaller devices instead of the "table".

However, you could go with this solution: UITextView change height instead of scroll

Another solution would be to check what happens when the cell with 0 height is shown. Check viewWillLayoutSubviews() and the constrains in IB.

Hope this gets you one step closer to the solution/answer.

Community
  • 1
  • 1
Darkwonder
  • 1,149
  • 1
  • 13
  • 26