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

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?

Related

UITableViewCell prevents UILabel from expanding height

I have a TableViewController with a custom UITableViewCell containing a single UILabel. The label will receive variable lengths of text, and should resize in height accordingly. I want to use auto layout, iOS10++.
However, it seems that the cell is preventing the label from expanding its height.
I have constraints on the label to pin top, bottom, left and right to the cell's contentView.
The label number of lines = 0, and is set to line break mode = WordWrap.
I have set the self.tableview.rowHeight to UITableViewAutomaticDimension, and have set the estimated row height to various sizes with no success.
I have increased (and decreased) the label's content hugging priority and and the vertical compression resistance, but this has no effect.
This sounds like a duplicate of so many other questions, but none I have read has solved my problem.
Some clues I have noticed:
1) If I remove the label's bottom constraint, the label expands correctly, but (of course) the cell doesn't expand, so the label cannot be fully seen after it expands below the bottom of the cell. So I conclude that the cell is preventing the label from expanding.
2) if I rotate the tableview to landscape and back to portrait, the first cell expands correctly. So something that occurs during the rotation solves the problem at least for the first cell, and also proves that the cell and label can expand as required.
I feel something is not right, but cannot figure it out. I am very close to going back to the old version of calculating the height manually and returning it in heightForRowAtIndexPath delegate method.
I would appreciate any suggestions. Thanks in advance.
I finally figured it all out.
In summary, I was configuring the cell (including setting the label's text) in tableView willDisplayCellAtIndexPath...
But it seems (obvious really) that for the autoresizing to work, the cell must be configured in tableView cellForRowAtIndexPath.
Moving the configuration into cellForRowAtIndexPath and suddenly everything started working perfectly.
Hope this helps anybody who has the same problem. I struggled with it for days.

UITableCell with auto layout configured multiline UILabel being truncated

I'm wrestling trying to get some multiline labels behaving inside tableview cells. I've already moved away from stacks that didn't work reliably at all as described in this question
UILabel inside nested UIStackViews inside UITableViewCell sometimes truncating
I fixed one of my views with the help of the comments on that question but another view just wouldn't work for me, even after moving to an autolayout non-stackview setup. I then ended up moving the cell that i couldn't get to work into the view which was working with similar layout and got to a point where i have two cells in the same view, one that worked and one that didn't. I've exported this into a new test app which I've uploaded here
In this app there is a simple tableview with 2 cells within it. One cell displays the large multiline text properly and expands the cell as required. The other cell stops short and truncates the multiline label as you can see here
The two cells to me seem pretty much identical in their constraints so I'm very confused why one works and the other doesn't. Here's an overview of their constraints
There are a few constraints not installed as i've been experimenting trying to figure out what's causing one of the cells to not work.
If anyone could explain to me what is causing these two cells to not behave the same, or more importantly why one of the labels doesn't fill its table cell that'd be really appreciated as I've spent hours looking and just can't seem to figure it out.
Cheers!
Edit:
To explain what went wrong in the first place...
The Right-side "Tags Label" in Cell2 has an Explicit Preferred Width = 300. I don't know the internals, so can't say exactly what's happening, but I get the impression Auto-Layout will take that Preferred Width value into consideration when calculating the text bounding-box height, and then continue on with constraints, content, intrinsic size, etc.
Simply un-checking that explicit option will fix the issue.
Original Answer:
I found it easier to start a new Cell from scratch, rather than try to modify the constraints you had set up, so... This will hopefully be reproducible.
Prep: Add new “Cell3” class; make basic edits to code to accommodate Cell3. I also find things easier if I make some variables for label values and set background colors of elements for easy visual inspection and testing.
Step 1: Add a new prototype; purple background; TableViewCell3 class and “Cell3” reuse ID; stretch it vertically to make it tall enough to work with (it won't affect run-time height).
Step 2: Add a UIView for the Left Side labels. Leading = 8 to Superview. Width = 200; Height = 100; Center Vertically. The Height and Width values will be changed later.
Step 3: Add the two Left Side labels - 1 and 2 (Body font) - to the UIView. Constrain 1 Left = 0; Top = 0. Constrain 2 Left = 0; Bottom = 0.
Step 4: Add a vertical spacing constraint from 2 to 1 of 7.5 and change the Height constraint of the UIView to >= 20 (runtime will likely always exceed 20).
Step 5: Change the Width constraint of the UIView to >= 40 (runtime will likely always exceed 40); Add Trailing Space to Container constraints for both Left Side labels, and set them to >= 0.
Step 6: Add Top and Bottom “to Superview” constraints to the UIView of >= 0.
Step 7: Add the Right Side label (Caption 1 font, number of lines 0). Constrain Top >= 0, Right = 0, Bottom >= 0, all to Superview; also Center Vertically.
Step 8: Add a Horizontal spacing constraint from Right Side label to UIView, set to 20. Give Right Side label a Width constraint = 40 (runtime will likely always exceed 40), and set the Priority to 250. This allows the UIView containing the Left Side labels to be in control of the widths.
Step 9: Assign IBOutlets and run the app. Try changing up the text. Make the left side labels shorter or longer... try setting the right side label to only enough text for one line... etc.
At this point, things should look pretty good - until... you put too much text in one of the left side labels, at which point you'll have a very, very narrow, very very tall right side label. So...
Step 10: Add another Width constraint to the UIView and set it to <= 200. Depending on your actual content, you may want to modify that - or perhaps set it to <= to a percent of the width of the cell.
I updated my original GitHub repo so you can check it out. It should have a commit for each "step" listed above, which might make it easier to follow along - https://github.com/DonMag/CellTest2
Results:

How to keep auto-sized table cells format consistent after edition/deletion of rows using UIKit?

I have a simple table view with a unique label for the cell prototype. I have been following the recommendations in order to have proper auto-sizing cells:
setting auto-layout constraints for the label regarding its leading, trailing, top and bottom spacing in its content view,
setting the rowHeight property to UITableViewAutomaticDimension,
providing the estimatedRowHeight property (set to 44),
setting the number of lines for the label to 0.
I also have enabled the default Edit Mode which allows the reordering and deletion of rows (exactly like in the Alarm tab of the Apple Clock app).
Now, on to observed problem:
I provide two labels, one with a short text and a second with a longer text, so that the second row has twice the height of the first row.
I enter Edit Mode, and swap the position of the rows. So far, everything keeps its composure and the sizing is still correct, the longer label being in the top row.
When I swap the positon again to return to the original layout (short label on top and long label on bottom), the longer text is not spread over two lines in its row as it should, but turns into a one-line display with an ellipsis at the end. Moreover, the height of the cell remains the same and does not fit the now single-lined text.
And after exitting Edit Mode (compare with the first image):
I suspect that I might be missing some kind of reload operation after the moving or deletion of rows, but I did not find the relevant information. Similar sizing problems appear when deleting rows that have been previously reordered.
How to keep the cell sizing as well as the text formatting consistent after moving/deletion of rows?
Thank you very much.

UICollectionView min spacing not followd

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.

UILabel that has unnecessary padding in a cell

I'm trying to make a Facebook clone for practicing iOS and I can't see why a label I have on my news feed gets unnecessary padding.
It only occurs on some labels, others on my news feed turn out fine. However for a select few there's a block of white pace above and below. At first I thought it was an alignment issue so I changed the labels background to green to show that the constraints hold out.
Anyone know as to why it's placing the padding, only for a select few?
By default UILabel will center its content vertically. Therefore, if label.bounds.size.height is greater than size of the text, the label's instinct is to center the content vertically, which will results in the vertical padding that you see in the attached image. Ensure that the label's height is being set according to the height of the text it contains and the problem should go away.
As dbart pointed out your label's frame is likely higher than the text needed. You can fix this by calling -sizeToFit. You can also check the amount of space the label is actually using for text by using +textRectWithBounds:maximumNumberOfLines:
If your cell is using autolayout to determine the height of the label you should set the preferredmaxlayoutwidth to an appropriate value (for example table width) before laying out the cell.

Resources