Layout errors when pinning UIView edges to UITableViewCell contentView - ios

Got a problem here which is driving me crazy.
In a storyboard I have a table view in a layout and I'm trying to pin the leading, top, trailing and bottom constraints of a UIView to the prototype cell's content view. Straightforward enough.
Whenever I do this I get these layout errors for the UIView in Xcode:
"Need constraints for: X position, width"
"Need constraints for : Y position, height"
Constraints on that view (Superview is contentView):
In the storyboard I have the following set on the table view:
Row Height: automatic
Estimate (row height): 50
And on the table view cell:
Row Height: 50
I've seen a few SO posts and other walkthroughs that state that pinning the edges is what to do, and as I understand Auto Layout, I've provided the necessary constraints and info.
How can I fix this?
(BTW: I'm running Xcode 11.3.1.)
Update: The cells of this table view will be fixed height. I'm looking for the cell to provide the height for its content.

UIView by itself has no intrinsicSize - so you are telling the cell's contentView to pin to a frame of 0 x 0.
If you are going to add content to the UIView, set the constraints on the content to control the size of the view and the errors will go away.
In the meantime, give the view a height constraint and that will resolve the issue.
Edit
For fixed-height cells... try starting fresh.
Add a table view - give it desired constraints
Add a table view cell prototype
Add a UIView to that cell (give it a background color to make it easy to see)
Constrain the view at Zero on all 4 sides (constrain to margins)
Set the cell Row Height to 50 (un-check Automatic)
Here's how it looks for me:

It turns out I had the table view and cell constraints and settings configured properly; the problem was further up the view hierarchy.
The table view was placed in a vertical stack view, and that stack view's Alignment property was set to center (for the benefit of other views in the stack). This was causing issues for Auto Layout in calculating the width of the table view.
The solution here was to add an Equal Widths constraint from the table view to the stack view. Et voila! The errors on the UIView within the table view cell's content view disappeared.

Related

Why do nested UIStackViews with UILabels require Y position and height constraints?

I am trying to sort out a problem we have in a UITableViewCell. There are nested UIStackViews with subviews like UILabel having an intrinsic content size. I created this example for simplified explanation of my problem:
Results in these interface builder errors:
Top Stack
View Need constraints for: Y position, height
Middle Stack
View Need constraints for: Y position, height
Bottom Stack
View Need constraints for: Y position, height
To my (and my collegues) understanding, the stacks' and table view cell sizes should be determined by the UILabels and their intrinsic content size.
The "Outer Stack View" has four space constraints to its superview (the "Table View Cell") with 0 as constants because ultimately the table view cell and all stack view sizes should be based on the content views.
All UIStackView have Fill for Alignment and Distribution attributes.
I just dragged it together from the library as depicted, no fiddling in the inspector. It is simple to reproduce.
Adding 0-space top and bottom constraints to a label in relation to its parent stack view does not resolve the stack view allegedly lacking a height constraint.
The suggested constraints of the interface builder are nonsense.
What is missing? I created an example project hosted on GitHub.
You have to set the OutterStackView bottom constraint to be greater than or equal to the bottom of the cells content view.
See the issue is that you are asking the OutterStackView to be the same size as your cell in the StoryBoard but since your internal labels don't have their final intrinsic content size yet, you cannot fulfill all constraint requirements.

Constraints ambiguously suggest a height of zero for tableview cell's content view

This warning is driving me crazy:
[Warning] Warning once only: Detected a case where constraints
ambiguously suggest a height of zero for a tableview cell's content
view. We're considering the collapse unintentional and using standard
height instead.
I have a UICollectionView inside a UITableViewCell. The table view cell has auto row height, and the collection view has cell size of 44x44. The collection view cell has default size. I have a button inside the collection view cell with constraints pinning it to the top, bottom, left, and right of the cell (I also tried manually setting its size to 44x44). You can see the rest of the constraints in the following image:
When I try to set the Table View Cell Row Height as custom equal to 44, I get an error that the constraints are conflicting and one of them needs to be removed. When I set it to 50, I get the warning above again.
Any ideas? :S
Thanks!
make sure in your cell class to define constraints in a such way that cell contentView will have a least 1px height/width (e.g. height constraint >= 1)
I do like this (collectionView is a subview of cells contentView)
collectionView.heightAnchor.constraint(greaterThanOrEqualToConstant: 1).isActive = true
CollectionView constraints are wrong, you added top constraint and also aligning collectionView center vertically, remove one of them. also take a look of screen short.

Autolayout. Set two views vertically with dynamic height

I have a view1 which can have dynamic height depending upon content with in it. Just below that view, I have to show the table view. Top space constraint of tableview is view1 and bottom constraint of tableview is view 3 that has fixed height and stick to the bottom.
The problem is that I can neither set height constraint of view1 nor tableview as top view can be dynamic and tableview have to take remaining height that varies in different device and I am getting error:
"Need constraint for Y position or height" for both view 1 and tableview. Although I have set costraint for y position for all views.
How should I solve this.
Add a constraint so that the top of your tableview is adjacent to your view 1.
Create a constraint for view 1's height. Doesn't matter what value you pick, just pick something.
Create an outlet for your constraint in step 2.
You should now be able to programmatically update the constraint in step 3 to have whatever value you really want for the height, and the rest should happen like magic.
4a. You might need to call layoutIfNeeded to make the view redraw and relayout things.

iOS - Expand UICollectionView height using Auto Layout as new cells are added

I am building a collection view that will allow a user to add data as they want.
The collection view will start out small (screen width, 30px height), and I want it to grow in height as new cells/rows are added. It should stop growing when it hits the bottom of the view controller's view.
The collection view is the ONLY view in the view controller (just for testing purposes and starts out just below the top layout guide (below status bar).
I tried adding the following constraints to the collection view, but it doesn't grow (and I have constraint warnings in storyboard of "Inequality Constraint Ambiguity"):
Leading Space
Trailing Space
Top Space to Superview
Bottom Space to Bottom Layout Guide >= 0
Height >= 30
What am I doing wrong? What constraints (in storyboard) do I need to add/fix?
I think you should tweak it in your code. You need to calculate each row height in your collection view plus the internal spacing between each row, and after a cell was added you call these methods
collectionView.frame = CGRectMake(a,b,c,d) // Adapt new frame to expand height
collectionView

Autolayout TableView Issue

I'm having a strange issue with my tableview, debugging the view hierarchy i'm getting this
I've tried a lot of constraints with no result, how should I setup my tableview to avoid that kind of misplacement ?
Edit
I've a custom cell with an image view, actually set just this 5 constraints
Top, bottom, leading and trailing values are 0
You just need to set Your UITableView constraint to Leading,Trailing, Top and Bottom. and also for your UIImageView set same constraint. so your imageview will show fit to cell as per table width. no need to set Align Center X constraint.

Resources