UICollectionView min spacing not followd - ios

I am explicitly setting the minimum cell spacing in the Storyboard to be 4, and the Storyboard's layout shows this:
But at runtime the vertical spacing is zero. (I have verified that the squares are still real squares.)
This has me losing hairs.

I ran into this same problem and was able to figure out what was going on.
If you are programmatically setting the size of the cells, make sure that the size of those cells is also updated in the collectionview of Storyboard.... Once these two places where the cells match, then the spacing can be created correctly.
Chances are the space is different because your cell width and height are greater in storyboard.

Related

Why won't the spacing between uicollectionviewcells equal to 0?

I'm using this library and I've tried messaging the library creator but to no avail. I've tried playing around with/changing lots of the code, however I'm having no luck in fully understanding what's taking place.
The problem: I am having problems making the spacing between the cells equal 0; for some odd reason when I set the collectionview item size to the screen width minus 30, there is still a huge gap between the cells despite me setting the minimumspacing to 0. Basically, the next cells, whose edges should be on screen, are not. I want them to be hugging each other practically, but it seems that each cell is centered and the spacing between the cells is the screensize - cell item size. Further, when I scroll through occasionally, next cell is hugging, and then immediately gets reset once it comes on screen. When I change the width of the cell to something a lot smaller, the spacing between them only grows.
I've tried commenting out offset / inset lines of code, but those only ruin the effect and don't actually make any change to the cell spacing. I've also tried to manipulate the values of the offset / inset. The only "solution" i've created is to use a negative value for the minimumlinespacing, but that only exacerbates the hugging glitch that occurs and makes the reset visibly drastic.
Any pointers?

aspect ratio for imageView in dynamic tableViewCell

I've got a tableView with 4 dynamic prototype cells: three of them for different kind of labels and one for an imageView.
In viewDidLoad() I use this code to determine the height of each cell based on their content:
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
The problem is for the cell with the imageView.
The picture to be displayed occupies almost all the cell and it needs to be shown in landscape keeping it's aspect ratio.
I was able to do it, setting top, bottom, trailing, leading and height constraints: in this way the tableView could calculate the appropriate row.height.
Switching from iPhone 6 to 4, the image width is reduced but the height remains the same resulting in strange ratios.
I need to calculate the eight of the view based on the width of the moment and I tried it in two ways:
creating an NSLayoutConstraint outlet for the height. If I assign it a static value it works but when I try to calculate like:
constraintOutletForTheHeight.constant = CGFloat(myImageView.frame.size.width / 2)
it starts showing the first cells with the old value and the others appear with the computed height value.
I also attempted setting the aspectRatio constraint from Storyboard but in both cases Xcode tells me that it can't simultaneously satisfy constraints.
Thank you for your interest
After a lot of println() I found where I was wrong and a solution for that (not the most elegant but at least it works).
I discovered that the starting width of the imageView is the one assigned via storyboard (even after I set the imageView.image property) and after a bit of scrolling, it switches to presumed value: this is why I got constraint errors.
As a solution in tableView:cellForRowAtIndexPath:, I used the tableView width property like this:
cell.<image_view_height_contraint>.constant = CGFloat(tableView.frame.size.width / 2)
If you know a better solution you're welcome otherwise I hope it could help someone else.

TableViewCell with multiple columns

I have an application in which I want to have a tableView with custom tableViewCell that has five columns.
I want them to be like 20%, 30%, 15%, 20% and 15% of the whole cell's width, because I want my application to launch on iPhone and iPad.
What is the best way to do this?
Now I have created a tableViewCell prototype with 5 labels. I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them.
Here is a screenshot.
You can create table view cell with collection view. By that, you can get different part in cell as well as you can change each column width programmatically based on your needs.
Thanks
A table view has only one column and allows vertical scrolling only. link
If you want to use a few columns, you can use UICollectionViewController
I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them
To fix it change width constraints to be a multiplier of superview width.

Why is contentView height of UITableViewCell always one point smaller than the UITableViewCell?

My cells, at the UITableViewCell level, have a height of 44 points. The contentView says 43 points, and it's greyed out in IB. How can I make the content the same size as the cell?
Note that I'm designing my cells in separate xib files.
I want my cells to have no margin between each other.
I have also set None as separator style of the table view.
Row height in IB is also set to 44 points. Not using any sections.
If you're using storyboards setting the Separator of the tableView to None (instead of Default) gets rid of the 1pt separator and the contentView's height becomes equal to the cell's height (even though it's grayed out)
Note: This solution doesn't work with xib files (at least doesn't update the IB, didn't check at run-time).
I figured it out. I was using full sized background images as background for the cells. As soon as I stopped putting separate UIImageView:s as background in the contentView, and instead assigned the background UIImageView:s to the cell's backgroundView property, and then set the height of the cell as well as the height of each cell in the uitableview (and also set the contentView background color to fully transparent!), things started working as I wanted them to. The contentView height will still be one point smaller, but now the cells align perfectly to each other. For this I don't need to have the contentView height match the UITableViewCell height.

Please explain the following auto layout behavior to me

I'm currently trying to figure out what I am doing wrong with the following constraint-based UITableViewCell layout (iOS 8).
My cell is laid out as shown in this image:
There is an image view on the left, a label on the right, and both should be touching the cell margins everywhere. The image has a fixed size (64x64), the label's height will always be smaller than that. I want the image's height to cause the cell to expand the height to the correct value (image height + 2 * margin).
The problem is this: I have three constraints for the vertical size, V[Image(64)], Reset.bottom == UITableViewCellContentView.bottomMargin and Reset.top == UITableViewCellContentView.topMargin (all defined via the storyboard). When I display the cell, I get an unsatisfiable constraints error. The UIView-Encapsulated-Layout-Height constraint interferes with my constraints, and auto layout breaks my image view height constraint. Everything looks as it should, but I don't like errors at runtime.
If I give my height constraint the priority 999, everything looks fine, no errors.
So my understanding is, that my height constraint will be broken in both cases at runtime.
But when I delete the height constraint altogether, the image displays at its original height (the file has different resolution than what I display in the cell). Why is this, or what am I getting wrong here?
Edit/Update: I just noticed I was wrong about everything working. The initial display of my cell is fine, but when the cell changes (in my case I am moving another, simple single line label, cell below it) my cell changes its height to the default row height (44 pts), squashing the image in the process. This happens in both cases, when auto layout breaks my constraints or when I reduce the priority of height to 999. I'm really at a loss as to how to get these cells to do what I want.
You should try setting the estimatedRowHeight property in code, and also, depending on which version of iOS 6 you're using, set the rowHeight property to UITableViewAutomaticDimension,
self.tableView.estimatedRowHeight = 44;
self.tableView.rowHeight = UITableViewAutomaticDimension;
The default rowHeight for nib or storyboard table views, is supposed to be changed to UITableViewAutomaticDimension at some point; I don't know if it has yet in 6.0.1, so that last line may or may not be necessary.
Are you supporting older version of iOS? If that is the case, be sure to uncheck the Relative to margin option for the constraints that are complaining.

Resources