UITableViewCell AutoSizing Issues - ios

I have a UITableViewCell I want to resize to content. Currently, it's cutting off the UILabel with all of the text. Also, it's completely cut off before scroll, and after it results to the below:
How do the constraints/content hugging properties need to change to allow the cell to size properly?
Ideally, the UILabels should push the size of the cell to be larger.
View controller:
self.tableView.estimatedRowHeight = 75.0
self.tableView.rowHeight = UITableViewAutomaticDimension

Ended up writing the constraints programmatically and ditching storyboards all together. Snapkit is nice.

Set the number of lines to 0 for the UILabel. It will allow for the text to appear completely.

Related

UITableView automatic dimension with UITextView max size of cell

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.

UITableviewCell Dynamic Height Programmatically with the use of UITableViewAutomaticDimension

Hi Guys here i am suffering issue of dynamic height of UITableview Programmatically. I have set Autolayout of all view without any error
but lblDesc (UILabel) not expand with its size according to content. plz guide me
sometimes I do not need btnVideo (UIButton) so i have to hide btnVideo(UIButton) and dynamically all social networks UIButton come after lblDesc (UILabel)
Here i attached my code with only one view
UITableViewAutomaticDimension
https://drive.google.com/file/d/0B5mabdphYDhzWG9UMzM2MTV6cms/view?usp=sharing
You should use UITextView instead of UILabel.
Btw, never forget you setup constraints between text view and container views for all edges correctly, so according to your text size, the whole cell will be resized dynamically.
I have reviewed your code. tableview has correct setting.
Just setup correct layout constraints so that table view could know how it should change the height of cell.
Cheers

iOS 8+ - UITextView Not Stretching Vertically in UITableViewCell

I am using Auto Layout and have a UITextView in a UITableViewCell. The UITableViewCell's height is calculated automatically based on the constraints. Here is how I am specifying that the cell should be self-sizing:
override func viewDidLoad()
{
...
// Set the estimated row height
self.myTableView.estimatedRowHeight = self.myTableView.rowHeight
// Set the height of the cell row based on its constraints
self.myTableView.rowHeight = UITableViewAutomaticDimension
// Prevent extra rows from displaying
self.myTableView.tableFooterView = UIView()
...
}
I am certain that all of the constraints (leading, trailing, top, and bottom) are set properly on all of the UI elements on my UITableViewCell, but for some reason the UITextView is not stretching to fit its available text. I've tried reloading the layout and the constraints in the cellForRowAtIndexPath and the willDisplayCell methods to no avail.
If I scroll down on the table and then scroll back up to a given cell, the UITextView DOES indeed stretch properly. But upon first load of the view, it is not acceptable for some of the text to be cut off.
I've tried just about everything I can think of, and I cannot get the UITextView to stretch. Even stranger, I made a test app, the UITextView did stretch properly. It's not working on my app for some reason, though.
I'm pretty stuck and could use some help. Any help would be greatly appreciated!
I found out that my UITableViewCell had a Disclosure Indicator accessory. All I had to do was remove the accessory (set to None), and everything is working great! I'm not sure why this works, but it seems like it's a bug. Self-sizing cells should work regardless of whether or not there is an accessory on the cell.
Note
This bug appears to have been fixed in iOS 9+. For apps that need to support iOS 8, I would recommend removing the accessory from your UITableViewCell and replacing the accessory with a custom image.

UICollectionView inside of UITableViewCell - AutoLayout

I'm using a UICollectionView inside of a UITableViewCell. It's all working fine, but i have some issues with Auto Layout. The UICollectionView should just show the cells without horizontal or vertical scrolling.Because now I have a the scrollable UICollectionView inside my UITableViewCell the following code doesn't work for me to get the dynamic table cells' height for this cell, but for other cells.
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44
I unchecked the option "scrolling enabled" in storyboard, but the result is, that i just can't scroll and don't see the rest of my collectionView.
Right now I simply set constraints from my UICollectionView to the UITableViewCell to all edges, but it's not working.
Would be great if someone have an idea.
Thanks a lot.
You'll need to have a height constraint set on your UICollectionView. If you've just pinned your UICollectionView to your UITableViewCell, it's getting its height from the table cell and the table cell is getting its height from the collection cell, so neither really knows what to do in that situation.
I did meet a similar issue before. It turned out that I haven't fully set up the relationship between the UITableViewCell and its containing elements so that they don't know how far to grow. I don't think this is related to embedded scrolling view. Just make sure that you have constraints set up between the UI elements inside the UITableViewCell and the bottom of UITableViewCell.

Managing dynamic height for UITableviewCell using iOS 8

I am trying out autoLayout features for dynamic height for UITableViewCell in iOS 8, I followed this blog http://www.appcoda.com/self-sizing-cells/. So here they mention that from iOS 8 managing dynamic height is hassle free.(In the link they have used swift, but I am using objective C)
Now after doing autolayout from xib, then we need to just write this lines of code in viewDidLoad
- (void)viewDidLoad
{
m_tableView.estimatedRowHeight = 83.0f;
m_tableView.rowHeight = UITableViewAutomaticDimension;
}
This is fine and my tableView height changes dynamically based on the content.
I want to know how to have more control on UITableViewCell height.
1) In my app, I want the cell height to be based on the content(data), but I need to have a default height, which will be applied if the content height is less than default height.
2) I want to know what will be the final height of the cell, before displaying to the user, because I want to add some UI elements below each cell.
So what functions I need to use to fulfil my tasks.
You would need to make sure your Storyboard prototype cell has NSLayoutConstraints from top to bottom. Dynamic cell sizing compresses whatever layout constraints you have in place to their maximum compression given the content set in cellForRowAtIndexPath to calculate the final rendered height of that cell. It is very convenient but it also means you need to be careful about setting your constraints correctly.
If you are adding any subviews programmatically, you need to ensure the same rule of thumb is true- there is a complete, deterministic set of constraints from the top of your cell's contentView to the bottom. Additionally, you will need to set any subviews' added in code translatesAutoResizingMaskIntoConstraints property to NO.

Resources