Interface builder constraint error - ios

A picture tells a thousand words, so here's the image of my UITableViewCell with my constraints.
I have a view pinned to all edges of the content view, and inside there's another view with a fixed width and height, pinned to the bottom and top of the superview. I am trying to center the x axis of this view to its superview. Running on a device everything looks okay, but interface builder is throwing an error on the outer view, saying it needs constraints for Y position or height. Can I somehow bypass that in interface builder? Or am I missing something fundamental about self-sizing table cells?
I am sizing my table cells with:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Don't worry. You have done everything right.
The warning is about the ambiguity raised due to two different heights of the tableViewCell.
Height 20 arising due to your constraints.
Height provided (or default height i.e. 44) in the size inspector.
Just change the cell height to 21( or 20 if your table view has no separators). The ambiguity would be resolved.
Note : Its 21, because the cell height is 1 + the height of the cell content view. This 1 point is for the separator line.
Also even if you don't do any changes the UI will be fine as this ambiguity would be solved at run time where height is calculated fro the content. Hence height fed in the IB is overridden by the calculated height, hence no issues.

Related

Expandable UItableview content height are different during expanded or collapsed

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.

Making certain number of table view cells fit on any device size in interface builder

How would I make it so that the table view cells have a certain aspect ratio.
How do I achieve this.
If possible, please given an example of w:h 16:4, and is it possible to set this aspect ratio in the interface builder on the storyboard.
Any help is much appreciated. Thanks in advance.
You can do this by giving the tableView a height proportional to screen and in heightForCellAtRow
return heightOFTable/numberOfCells
There should be a delegate function you can call to adjust the height of the cells based on the indexpath. something like:
func tableView(_ tableView: UITableView, heightForRowAt
indexPath: IndexPath) -> CGFloat {
return self.tableview.frame.width / 4
}
You can do it in Storyboard like this. Embed a UIView in your template cell, you can make it transparent and put it in the back. Stretch it to the edges and set constraints to all four edges. Now, make the a width constraint with same width as its superview. Then add an aspect constraint 16:4 with a priority of 999. The priority is needed to resolve a layout conflict you will otherwise get because of how sizing is working.

How to resize UITableViewCell height by showing and hiding components using auto layout

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.

UILabel constraints in UITableViewCell change in landscape

I've got a static UITableView in my storyboard that has a combination of system cells and custom cells. In order to make the custom cells look like the system cells, I've added a 15 point constraint from the front of the UILabel to the cell's contentView (to match the default separator inset of 15 points).
This strategy works well when the table view is portrait, but it looks like the constraints shrink when in landscape, as seen below:
Portrait:
Landscape:
You can see in the above image the "BUG REPORTING" section title, "Report a Bug" label (which is positioned by the system cell), and the line between the two cells are all 15 points away from the contentView's left side, but the "Logging" label is closer.
I've tried constraining the labels to the contentView with margins on and off, and the result is the same. The constraints for the custom labels even report different constant values when they're obviously the same shorter distance from the contentView's left side.
Does anyone know what's going on?
Try creating an IBOutlet for the label's leading constraint, and modifying it before the cell is displayed:
func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
let leftInset = cell.separatorInset.left
cell.labelLeadingConstraint.constant = leftInset - 8
// - 8.0 accounts for constraint to margins in autolayout
}
Or, you could use size classes to set a different constraint for the Compact Height size class:
Then, simply set the constraints' constants to 12 instead of 7 (or 20 instead of 15, if margins are not used).

Create a view table with static cells of equal height to fill screen

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

Resources