I have a collection view which displays cells looking like a classic table view with image, title and subtitle.
I want to be able to switch the layout to a grid showing only the images, 3 in a row.
The content of the collection view cell is layouted in a storyboard with auto layout. The imageView has the following constraints:
Leading 0 to the left cell edge
Top 0 to the top cell edge
Bottom 0 to the bottom cell edge
Fixed width
Horizontal space to the labels
After the layout change the imageView should have constraints of zero to all edges of the container, so that it fills it completely.
At the moment when I change the layout the app crashes with Unable to simultaneously satisfy constraints.. What is the best way to fix the constraints when the layout is changed, maybe even replace the cell class?
Turns out the problem was in some other part of the code.
You can change the collectionView layout with setCollectionViewLayout:animated:completion: and reload the visible cells in the completion block.
Related
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.
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.
I have a UITableView with various cells and I want to insert lateral space (to the right and to the left) between the cell and the View, but I am not able to do that.
Can anyone help me with this?
You should use a UICollectionView instead.
With UICollectionView, you can set the Cell's Width and Height, and add custom spacing between each cells. You can also configure the space between the cell and the border of the CollectionView.
If you really need a UITableView, try to add a Leading Space To Container Margin and a Trailing Space To Container Margin contraint between your UITableView and its Superview.
Here is an example : my tableview has a grey background, my cell has a yellow background and the superview has a blue background. Thank to the two constraints, there is a space on the left and the right of my cell
Is that what you need ?
One way is to make the TableView's width shorter that the controller's view width and add constraints to keep the space exactly as you want it.
Another way is to design your cell with an "internal container" with spacings to edge of the cell's content view.
You can easily do this in a storyboard using a container view…
Add a container view to your view controller, set the left and right constraints to the inset you need, add a UITableViewController and then drag from the container to the table view controller and embed it
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.
I searched a lot for a solution but can't find anything to solve this particular case.
I created a custom cell in a UITableView, with these elements in order from top with the constraints:
imageview (constraints top screen, fixed width, fixed height, centered horizontal)
top-label (constraints top imageview, fixed width, centered horizontal) Line set to 0.
view (constraints top label, fixed width, centered horizontal) The height is set to 0.
button (constraints top view, fixed width, fixed height, centered horizontal)
bottom-label (constraints top view, constraints bottom screen, fixed width, fixed height, centered horizontal)
The cell is autosized in height correctly, also if I insert a text for top-label very long.
Now I want to attach an action to the button to enable the resize in height of the view, like an accordion. So I'm trying to change the height with no success, anything change also if I reload the tableview.
I tried to set a constraints to the height of the view, but If I change that all the content move but the height of the cell doesn't change.
The only way I can have the view changing the height is setting the the rowheight of the table, but that change all the cells of the table and I want to open the view of only one cell.
Is there another way to do that?
Thanks in advance
Ale
EDIT:
For clearance I've solved using the delegate method suggested by noobular, setting the view to 0 height permit to have that expanded when the height of the cell change.
I achieved a similar effect by these methods. First, I determine a constraint that will represent the height of my cell. I'm using the height constraint of a decorative bar on the left hand side that's pinned to the top and bottom of the cell. It's important that this constraint belong to an object that is a child of the cell's content view and that will be on-screen in both open and closed states. This constraint is connected to a property on the cell it will control.
When I want to change the height of the cell, i.e. to 100, I can do so as follows:
cell.heightConstraint.constant = 100.0
cell.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
The calls to beginUpdates and endUpdates force the tableView to layout its cells again, thus expanding or contracting the cell. Note that I'm actually calling the first two lines in the cell's own setSelected:animated: function and the remaining two in the table view controller's tableView:didSelectCellAtIndexPath: method. This has the effect of expanding a cell when it is selected.
Edit: For auto sizing cells where you want UIKit to automatically calculate the height, I would suggest researching UITableViewAutomaticDimension
Implement this method for your tableView delegate:
tableView:heightForRowAtIndexPath: