I added a UIView above a tableview to make it scrollable and have some tableviewcell below it but I can't figure out how to set "Detail Header" view's height dynamic so it fits to my content. I have a textLabel inside which increases its height because of this
All constraints inside the View are fine but somehow that UIView doesn't adjust to content and it keeps its fixed size
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return detailHeader
}
As per my understanding of your question - how to set a table view header height dynamically -
at viewDidLoad method, please use below lines:
tableView.estimatedSectionHeaderHeight = 120
//open var sectionHeaderHeight: CGFloat(default is UITableViewAutomaticDimension)
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
override viewForHeaderInSection and use Auto Layout to constrain elements in your view as seen fit. That's done.
"doesn't allow me to put images yet" what you mean? As per my understanding if you are using Storyboard and Drag and Drop Image so it will assign an image straight forward using image option on the right pane.
This is late... but I had this exact problem with setting the height of a regular UIView above table view cells inside of a Table View.
I solved it by creating a reference to this top UIView in the view controller holding the table view. Then I just set the height to it dynamically based on the my criteria.
Example code:
topView.frame.size.height = 50
tableView.layoutIfNeeded()
Related
I have created an expandable tableview by separated with multiple sections. Because this tableview is inside a container view, so I need to update the container view size every time the tableview expand or collapse.
I update the container size with tableview.contentsize.height but I found out the content size are different when its expanded or collapsed.
For e.g, lets say there are 4 sections, if I collapse the 3rd and 4th sections the tableview will return me height of 200. Meanwhile if I expand the table just 1st and 2nd section while 3rd and 4th is collapsed, the content size height is more than 200.
I find it very frustrated why the program is giving me such confusing information, because I need a proper content size height to update the container view. Anyone here can enlighten me?
Instead of using the contentSize try calculating the height on your own. It'll be much easier if every cell is the same height. Let's say they are all 50 height and there is 1 row in section 1 and 3 rows in section 2. (1 + 3) * 50 = container height. Obviously if you have sections and insets you will also need to include that into your calculation.
If your cells are different heights, just call the table views cell height delegate to get the relevant height.
If this doesn't work for you then please be more descriptive and add some code.
The behavior of content size that you've described is correct. If you want to know the height of the UITableView you should look at it's frame, not at it's content size.
I'm not sure why you need a container view for the table view, you can layout it like the simple UIView, then you'll have one less problem, just change the tableView frame.
Update:
After reading comments i see that you want tableView inside of tableViewCell. Most part you'll have to do yourself, but i can help you with UITableViewAutomaticDimension.
Try adding this to your viewController:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAt indexPath: NSIndexPath) -> CGFloat {
return 44//approximate cell height
}
The main thing is to set constraints properly in interface builder. And remember that constraints won't change the height of the cell on builder, you may have to do it yourself in the Size inspector. Anyway if you won't - it will be resized during the build.
I'm having a problem with my table view cells as they do not adjust automatically with its content.
I have a label for a title and another label for a name. There is a text view below the two labels which is never displayed when the simulator runs.
This is
what the Table View Cell is supposed to look like, however, this is what the Table View Cell displays.
I have pinned all elements inside the table view cell to the content view using constraints. I read up that adjusting the table view cell height itself will not work, so, I have to adjust the height from the table view itself.
It is set to automatic but it is not adjusting as seen here. I have also tried to set the estimated height to automatic but to no avail. The only solution was to set a custom height but it would look extremely weird if the text view contains only a few text as there would be a large white space. I did not add any code at all to adjust the size.
These are the following constraints:
Table View
Name Label
Title Label
Text View
First You need to add height constraint for textview and add its IBOUTlet then you need to override the updateconstraint of cell and do following in update constraints method.
override func updateConstraints() {
super.updateConstraints()
self.textViewHeightConstraint.constant = self.textView.contentSize.height
}
and also for name label add bottom constraint.
By default the UITextView will not resize itself to fit its content. While you could use #Waqas Sultan approach, I would recommend to let the textView size itself. To achieve that, just use this:
textView.isScrollEnabled = false
Or, in storyboards, find the Scroll Enabled attributed and uncheck it.
This would make textView to size itself.
However, from the constraints you show it is hard to tell if there are really enough constraints to determine the proper frames for all the content - there are constraints related to Review label, but who knows how you constrained that label.
Not seeing all the relevant constraints in the tableView cell, I cannot guarantee that this will be enough to make it work as you expect (you might have forgotten about just a single one constraint, and it might be messing up your whole layout).
Hey buddy i would like you to try this way.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == desiredIndexPath { // the index where you want automatic dimension
return UITableViewAutomaticDimension
} else {
return 100 // the height of every other cell.
}
}
note: Make sure that you do not give height to the label. Otherwise the label wont expand according to content.
Evening,
I have the following design:
red box: is a table row
orane box: is a collection view
blue box: is a collection cell
When a collection view is empty I would like the row, or the collection view to autoresize to 0.
Currently I got this:
So I want the orange box autoresizing to height = 0 when there aren't blue box.
I know that I should use a constraint of collectionView Height and set to 0 when is empty.
But i have the outlet in the table cell and the collection delegate into the view controller. And I don't know how to accomplish this.
Any tips? With some code example please :P
I don't think it's possible to resize collectionView depending on its content automatically.
What you can actually do is add constraint of height to your CollectionView and set it to 0 if it's empty.
Then, your cell will have intrinsic height, if you connect CollectionView bottom and top to it accordingly (which you possibly already done)
The last step will be to set tableView.rowHeight to UITableViewAutomaticDimension. Probably it will work :)
Try to use
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat
function. In this function, for the corresponding row, return 0.
I have 4 components in my cell.
UIView
UITextView
UIImageView
Another UIView on top of the UIImageView
UIView
UIView
These content can be changed according the data that comes from the server(Sometimes it needs to hide the 3rd image view, sometimes 5th UIView). I'm using auto layout and with the auto layout what is the way to change height of the cell?
You can achieve this easily by using UIStackView. Stack views are a powerful tool for quickly and easily designing your user interfaces. Their attributes allow a high degree of control over how they lay out their arranged views.
Here is link for tutorial - Tutorial
If you hide a view from stackview it conveniently disappears from the layout, but it's still a subview of the stack view. So you don't have to do anything special if you want to bring it back later on. And by using self sizing cells, cells will automatically expand or collapse based on stack view height.
Do following steps:
When you hide any component give that component frame height as 0 and reload tableview. If you giving any component constant height then take outlet of height constraint and make it zero and specify its constant height again when you unhide.
When you unhide give him specific frame height and reload tableView.
heightForRow must returnUITableViewAutomaticDimension
As you already taken components from the storyboard so compiler understands the height of cell and work accordingly.
If you still facing issue you can ask.
this two methods are use for dynamic cell height
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
I post this for other man meet my issue.
Do not apply any change that will change the cell height in layoutSubviews, like hidden view or change any view's height.
How do I create a table view with static cells, where the row heights are all equal to make the set of static cells fill the table view?
The row height needs to be adjusted dynamically to account for the different iOS screen sizes.
For example I want a table view with 7 static cells of equal heights that fill the rest of the screen space except the navigation bar.
I've tired using the following with no luck:
self.tableView.rowHeight = self.tableView.frame.height/6;
and
override func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return self.tableView.frame.height/6;
}
and the autoLayout feature in storyboards is grayed out when I try to use it with any part of the table view.
Appreciate any help thanks!
Define auto layout constraints for your prototype cell.
Specify the estimatedRowHeight of your table view.
Set the rowHeight of your table view to UITableViewAutomaticDimension.
If we express the last two points in code, it looks like this.
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension