UITableView automatic dimension with UITextView max size of cell - ios

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)
...
}

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.

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.

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.

Related

How to auto adjust cell’s heights depending their text inside?

I am using a tableview to display a list of articles. Each cell needs to show an image and a text that has a brief descriptions of articles. What I want is to adjust the cells height depending the description length.
I know that I can resize the cells with the delegate method heightForRowAtIndexPath but I still don’t know which height to return.
Any help?
Ok, you can do that just using Interface builder. On the Tableview properties click “Row height” automatic.

Then you can use a UILabel with property lines set to 0 to display your text. When adding the constraints to the cell you need to make sure that the height of cell depends on the intrinsic content size of the label, basically is the size of the content (the text inside the label).
For example:

Here I have added a UIImage with constant height and width and with top and leading space to 0. Also I added a UILabel with top/bottom/leading/trailing space to 0. Here the cell height will depend on the UILabel intrinsic size that is what you want. Also to prevent glitches with short texts you can add a minimum height size to the UILabel.
Of course this is an example, you can achieve the same using other constraints. The main thing here is to be aware of the concept of intrinsic content size: a predetermined size of a view based in its content.
Let's say you have a textView in each cell. The width of the textView is fixed. And you know the text for each cell. Base on the info above, you can get the height of your textView by:
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
Then you should be able to calculate the height of your cells base on your layout and stuff.
The question is answered but just in case you want to know... You can return UITableViewAutomaticDimension in heightForRowAtIndexPath.
Nevertheless you should google more before asking a question that has been answered multiple times on the web (and on stackoverflow) ;)

UITableView rowHeight UITableViewAutomaticDimension cell wrong height after first load

I've got a UITableViewCell subclass that has two labels of variable height. AFAIK, the autolayout constraints are correct. The first time the cell is rendered in the table it's correct, but any subsequent rendering is blown out too tall. Even after the parent view controller is deallocated! It literally requires an app restart to go back to normal.
I've tried clearing text of the labels in an override of prepareForReuse in the cell class.
// viewDidLoad
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
To support dynamic layout with textview, a height constraint is needed and that needs to be updated in layoutSubviews method(you need to subclass UITextview).
In addition to this, you need to make sure you have a UITextview bottom constraint with cell's contentView.
Sounds like your bottom constraint of UILabel is getting increased. Check if you are updating the bottom constraint constant value.

Scrollview doesn't scroll with multiline label in it

I have a multiline label inside a scrollview. I set up the content size, let's say to scrollView.contentSize.height = 2000
But the view doesn't scroll. There is barely any code in the project. What is going wrong?
The only thing is that I don't have constrain for the height of the label, because it will vary depending on the length of text.
It doesn't matter about the height. But what does matter is that you need to pin it to the bottom of the scroll view also.
By pinning it on the top and bottom it will use the label to set the content size and so allow it to scroll.
I suggest to add a UITableView instead of UIScrollView, adding one UITableViewcell that contains a UILabel. By setting the appropriate values of:
Label's constraints.
tableView's rowHeight.
tableView's estimatedRowHeight.
It should works fine for your case.
For more Information about setting a dynamic cell height, you might want to check this answer.
Hope that helped.

UITableView inside the UIScrollView for horizontal scrolling does not work as expected

I use a table view which has got the cells with varied set of information and which can not be contained with in the frame of the tableView. So I do need to have a horizontal scrolling capacity in the table view.
Since horizontal scrolling is not possible with tableview,I planned to use a scrollView with horizontal scroll. I added the tableView on top of the scrollView.
The problem I face is irrespective of the content size property of the scrollview, the scrollview never scrolls beyond the contentOffset value X of 256-320 pixels.
I am planning to achieve the effect as it is in the below screen shot
Try two things,
First Set the TableView height as total numberOfRows :
tableView.frame.size.y = numberOfRows * 44;
now disable the tableView Scrolling
and set he scrollView contentSize as per your need;
scollerView.contentSize = numberOfRowsintableView
Surely it will solve your problem
Set proper ContentSize for ScrollView in ViewDidLoad.
scollerView.contentSize = CGSizeMake(Width,Height);
Calculate the Width as Per Requirement And Height (you are using Tableview So You can take Height same as View Height)
Then it will Surely Scroll.
I implemented the entire functionality using code without using interface builder. It works as expected now. I did not know why it did not work when used with interface builder.

UITableView scrollable content size

I am trying to achieve a newsstand like effect with a scrolling and repeating background. I could do this using a UITableView if I could set it's content size (An inherited method that seems to be overridden by something else in the UITableView Code) in order to fill the view with unscrollable cells.
Currently I am planning to make a custom uiscrollview. Which will be more complex and won't have the cell reuse.
So, is there a way to set a UITableView's content size.
You could semi-hack a 'contentSize' kind of control over a UITableView by simply setting the number of cells that you have (let's assume you have a 1 section table view and aren't using a 'grouped' styled table) to:
your desired height contentSize height / height for each cell (default is 44)
Now, your problem is that if you only have, say, three rows' worth of data to display, what to do with the rest of the cells? Well, simply set their backgroundColor to the same as the table's background color and their selectionStyle property to UITableViewCellSelectionStyleNone. Wham, now you have invisible cells, and can set the 'contentSize' of the table.
Note that your contentSize will only have a resolution of the height of your cells, if you leave the cell heights at their default 44, then you can only set the contentSize to a multiple of 44.

Resources