Swift Stevia + UICollectionView self sizing Cells issues - ios

I've spent a few hours figuring out why Stevia and self-sizing cells have terrible sizing issues. It seems that the constraints are conflicting with the auto-sizing. Causing the cell to snap to the wrong size.

So after a lot of trial and error. This is what finally worked.
Set UICollectionViewFlowLayout:estimatedItemSize to UICollectionViewFlowLayout.automaticSize. No other setting seems to work.
Set UICollectionViewCell width and size explicitly to the target cell size.
Do not rely on contentView as your view container. Instead, add a new and also size that view to your target cell size.

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.

UICollectionView Different Item Height Issue

Keypoints:
I am using XIB with Autolayout
I am trying to create a UICollectionView that has a dynamic item height. This is needed because I have an UILabel that might have different heights due to it's length. The UILabel's text is set using it's attributedText property
I am aware that:
I MUST NOT implement sizeForItemAtIndexpath.
I MUST set estimatedItemSize to the flowLayout of my UICollectionView.
I have been scavenging Stackoverflow for more than 6 hours but still unable to find solution but most of the solutions are either emphasizing on Storyboards or intended for lower version of iOS.
The best one i have found so far is this UICollectionView Self Sizing Cells with Auto Layout
However, that also did not fix my problem because somehow, my cell does not have the intended width.
Any ideas please?
.
.
Please help me vote this up cause I am literally banging my head now. Any help is very welcome.

UICollectionViewCell updating constraints after adjusting layout in code

I am adjusting UILabels and an UIImageView's frame in code in UICollectionViewCell to fit smaller device sizes but I can't seem to scroll to the end of the UICollectionView. There are about two unreachable cells.
I suspect that it could be a constraints problem and I have tried calling updateConstraints or updateConstraints if needed but nothing visible seems to be happening. I make use of AutoLayout in the storyboard.
I think I know what the problem, because you are changing the Cells at runtime, after the contentSize already as been calculated you need to set it manually.
Try to set the collectionViewContentSize manually and tell me if it worked..

UICollectionView self-sizing-cell on iOS 8 will crash with UIDynamic flowLayout and repeating call to _updateVisibleCellsNow

I am trying to use the so called "self sizing cell" which means:
set estimatedItemSize on flowLayout
overide preferredLayoutAttributesFittingAttributes in cell class
Such as this: UICollectionView Self Sizing Cells with Auto Layout
And I need dynamic effect like this:http://www.teehanlax.com/blog/implementing-a-bouncy-uicollectionviewlayout-with-uikit-dynamics/
It works fine without UIDynamic, but I need UIDynamic. As what I see, it will call the prepareLayout and layoutAttributesForElementsInRect until die, there will be too many _updateVisibleCellsNow waiting in line.
I have no idea how to solve that, please help me in case u see. Or, if I am using those technologies in wrong way, please let me know.
Two things worked for me:
Make sure your collection view itself has layout constraints defined for placement within its superview.
I got this crash when the estimated size was larger than the final size. If I set the estimated size to a smaller value the crash stopped.
If any size or frame position changed, it will trigger all cells' preferredLayoutAttributesFittingAttributes until all cells' frames did not change.
The behavior flow is as the following:
1. Before self-sizing cell
2. Validated self-sizing cell again after other cells recalculated.
3. Did changed self-sizing cell
If second steps can't get stable all cells' position and size, it will become infinite loop.
Please reference my post here: UICollectionView Self Sizing Cells with Auto Layout

Swift: UITableViewCell size to width of parent UITableView with autolayout enabled

I'm experimenting with autolayout and am running into trouble with UITableViewCell since they're created at runtime. My cells are loaded from a xib from the main ViewController. This xib has View mode set to Aspect Fill.
I've read about different ways to do this online and have yet to get any of them working. What's considered the best way to handle this?
It looks like your constraints aren't set properly, as the cell is shorter than the image's height.
Using AutoLayout and self-sizing cells is the easiest way to handle what you want to do. Once your constraints are setup properly for your custom cell, tableView:cellForRowAtIndexPath: can call dequeueReusableCellWithIdentifier:forIndexPath: and all the subview layout will be handled for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He also provides workarounds for the minor issue with the initial cell width being based on the storyboard cell, instead of the tableView width. I worked around it by setting the cell's initial width to the tableView's width, as Rasputin had suggested.

Resources