Automatic height for TextView using autolayout in cell for tableView - swift

I want to set the height of the TextView as per content in a cell for tableview.
i pinned the top of text view to the job title UILabel and bottom to the content view (parent) but it does not working. is there any tutorial for this kind of layout positioning ?
How i add constraint to fix the automatic height issue?
Current constraints

As I can see you have some other setup as well in the cell. to support dynamic height
you can give fix height constraint to UITextView.
take outlet of that constraint.
update it before you return the cell from cellForRowAt method.
so, your cellForRowAt method will end up like
tableCell.textViewHeightConstraint = textView.contentSize.height
tableCell.setNeedsUpdateConstraints()
tableCell.setNeedsLayout()
return tableCell

Solved this way.
Change the TextView to UILabel, Then set Lines property in the attribute inspector to 0.
on viewDidLoad
tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 229.0
It works.
More https://www.raywenderlich.com/8549-self-sizing-table-view-cells

You can apply Self-Sizing Table View Cells by adding two lines:
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableViewAutomaticDimension
You should read this document from Apple:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html

Related

How to set tableview cell height equal to the table view that contains it in Swift 4.0

I have a UITableView inside of a cell, and that UITableView always show one row.
I need the height of that row to always be 100% of the height of the tableview that contains it.
I try this:
tableView.rowHeight = tableView.frame.height
In the awakeFromNib() func of the cell that contains the UITableView.
But does not work, the row be showed with ~75% of the UITableView heigth.
Any other ideas?
Problem: https://ibb.co/m0gwcNS

Setting the height: TableViewCells with custom Nib that are different sizes

I have a TableViewController that has a custom TableViewCell under the identifier "customCell". Heres an image of the configuration of the cell along with the IBOulets connected to it:
The cell takes information from my backend and presents it. The description text view (just noticed that I accidentally named it descriptionLabel) doesn't allow scrolling so it expands based off of the content that it's holding. The database is being processed correctly from the database, and it's displaying on the app. The only problem is that the cell is not its correct height. On the TableViewControl that's registering this cell through its identifier, I automatically set the height of the cell using UITableViewAutomaticDimension in heightForRow:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
but that's not working either. *The only thing that works is when I set the height of each cell to a standard value such as 200. That doesn't work for me because each cell will be a different height because of the length of the textView.
How do I set the height of a custom nib (tableViewCell) so that it adjusts based off of the content within it instead of setting the height to a specific value?
1-Remove both textViews and replace them with labels
2- Title lbl with theses constraints
top,leading,trailing , .lines = 0
3- Description lbl with theses constraints
bottom ,leading,trailing to contentView,top to bottom of title lbl , .lines = 0
Off course you can leave the 2 textviews , but you have to give each one an initial height and do this in
override func layoutSubviews() {
super.layoutSubviews()
self.titleTvHeight.constant = self.titleTv.contentSize.height
self.desTVheight.constant = self.desTv.contentSize.height
}
//
Don't forget to set this in viewDidLoad of the VC
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
Anyway you can remove heightForRowAt

How to change tableview cell height according to label text swift?

Using a UITableView in Swift, could someone please help me automatically change the height of the cell based on the label, picture, and description please?
I want to dynamically change the cell size as the label text increases. i have already applied autolayout on the labels.
Considering you have already applied constraints on the label. And also set your number of lines to 0. And then paste the following code in viewDidLoad method
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
If the code below doesnt work in method viewDidLoad doesn't work instead add the code below in method
ViewWillAppear
tableView.estimatedRowHeight = 60.0// the estimated row height ..the closer the better
tableView.rowHeight = UITableView.automaticDimension
plus :
The constrain should atleast define the width ,top spacing and bottom spacing not the height constrain.
In attributes set lines to 0
For making the cell auto sizeable, you need to do two things:
Bottom space and top space constraints will push the cell size as needed. number of lines of label is 0.
In your viewDidLoad() method add self.tblList.estimatedRowHeight = 50.0 self.tblList.rowHeight = UITableViewAutomaticDimension
Swift 4 answer:
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension
Please be sure that the constraints are set

Swift, Auto Resize Custom Table View Cells

In my app, I have a table view with an image, label and text view in each cell. I would like to be able to auto-resize the cells depending on the amount of content in the text view. (The text view is the lower most text.)
So far, I have added the correct constraints; leading, trailing, top and bottom to the text view and have disabled scrolling and editing.
In my tableViewController.swift file, I have written this code:
override func viewWillAppear(_ animated: Bool) {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
However, this is not working as when I add more text to the text view, it just cuts off.
Maybe this has something to do with the card like look, I have got a UIView in each cell acting as a card.
I honestly don't know what I am doing wrong
A picture is below of what my app looks like and if anyone could help that would be greatly appreciated
Check if your constraints are like this(based on your image) :
imageView : set to Top and Leading of your container, with fix height and width values.
label : you can set it to top and horizontal space of your image, with fix height and width as well.
textView : leading to image, top space to label, trailing and bottom to container.
And keep using
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
in your viewWillAppear()
Update for swift 4.2
Use:
UITableView.automaticDimension
Instead of:
UITableViewAutomaticDimension
Make sure that the content mode is set to Scale To Fill of your UITextView
Make sure that there are no height constraints for the UITextView and the card UIView
Try to add the estimated height into viewDidLoad:
override func viewDidLoad() {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
Maybe the AutoHeight is not working because of the UIView above the UITextView. Try to call the sizeToFit and layoutIfNeeded methods for the UIView in the cellForRowAt:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell
cell.vwCard.sizeToFit()
cell.vwCard.layoutIfNeeded()
return cell
}
You can also try the sizeToFit and layoutIfNeeded as well for the UITextView.
Hope this works........
Set bottom of your textView with bottom of that white UIView and make sure that white UIView has left,right,top and bottom constraints :)
Same type of example is explained here programmatically....

How to adjust cell size in tableView

I want to design a tableView like this
But when I adjust
In the cellForRowAtIndexPath, I put cell.contentView.frame.width = CGFloat(tableView.bounds.width - 40) to make the contentView of Cell smaller, but it shows error:
Cannot assign to property: 'width' is a get-only property
So how to make the cell smaller.
Any helps would be appreciated, thanks.
You can't directly assign to the width of a CGRect. A CGRect consists of position and size, and the size in turn consists of height and width.
In other words, frame.width is just a getter for frame.size.width
So the correct assignment would be:
cell.contentView.frame.size.width = CGFloat(tableView.bounds.width - 40)
But this altogether is a bad pattern.
You should design the cell as a custom cell, either in a new .xib file or as a prototype cell in the storyboard, and use AutoLayout to layout the subviews of the cell's contentView.
Use the heightForRowAtIndexPath method
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return calculatedHeightForSpecificIndexPath
}
To custom a cell like that image I posted, just create an UIView inside the contentView of cell, like this:
The result here: