Which method to setup UIview? - ios

The cells in my collectionView contains a UIView that should have a shadow. The problem is that if I set the shadow in the cell's AwakeFromNib method the shadow will be applied to the UIView before it has the correct size (which means the shadow will have a different size than the UIView). Which method is called when the cell and all its subviews have been correctly loaded and setup?

you can do any UI related work for collection view in this delegate method
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
It will always work

Related

Why CollectionView cell insider other collectionView has wrong size?

I have a collectionView (nested) inside a collectionViewCell. When I open this VC, the cell inside nested collectionView have wrong size. After scroll down and up - everything is fine.
I think when I am scrolling method layoutSubviews are calling.
But when I added
cell.setNeedsLayout() cell.layoutIfNeeded()
inside func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) the result is not change.
Constraints:
i think problem is not with the size, it is from constraints.
Would you please share a snippet of your code

UICollectionView cell creation bypassed `willDisplay cell:` call?

I have an UICollectionView with .layer.masksToBounds set to false.
I do this so that way I can achieve a fade-out effect of any cells that scroll out of the collection view's proper bounds.
In order to ensure that I don't display out of bounds cells (when the UICollectionView loads them in), I use the delegate callback:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
// pseudo code
if !cell.frame.intersects(collectionView.frame) {
cell.alpha = 0
}
}
However, it appears this callback is not invoked when the CollectionView populates an out-of-bound cell?
While obviously I am always informed if a cell is constructed through the cell provider function (i.e. the function where I invoke the dequeueReusableCell call), I cannot determine whether the cell I construct is out of bounds.
Is there any way I can be informed of when the UICollectionView constructs an out-of-bounds cell?

UICollectionViewCell inside UICollectionView

I am trying to place UICollectionView inside UICollectionViewCell. I have prepared my cell in xib file using autolayout, same for cells inside collectionView.
But what about autolayout?
Some of my UICollectionViews inside my cell are different size that the others because of its content so I fail using sizeForItemAt indexPath: to calculate proper size.
Is there any fairly simple way to do this using autolayout, different from changing this main UICollectionView to UITableView with UITableViewAutomaticDimension as a heightForRowAt indexPath:?
EDIT:
To explain everything i've done so far to make this work i'll provide a cell layout:
and some algorithm to calculate cell's size:
First of all i declared an array of cells heights inside my UIViewController subclass like so:
fileprivate var cellsHeight: [CGFloat] = [].
After that inside UICollectionView delegate method collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell i am populating this array with height of each individual collectionView content size like so:
let cell = collectionView.dequeueReusableCell...
if cell.collectionView != nil {
cellsHeight.append(cell.collectionView.collectionViewLayout.collectionViewContentSize.height)
}
And then in inside collectionView(_ collectionView: UICollectionView, sizeForItemAt indexPath: IndexPath) -> CGSize i am calculating each label size using boundingRect method, adding up some padding and previously fetched collectionView height.
But i think this is a pretty overkill.

Subview of custom UICollectionViewCell apparently not redrawn when cell is reused

I have a UICollectionViewController whose collectionView:cellForItemAtIndexPath: returns a subclass of UICollectionViewCell. This cell has a couple of subviews which are also configured as the cell's outlets (e.g. cell.dot, a custom UIView that draws a colored dot in its drawRect:).
What appears to happen is that (at least some of) those subviews are not refreshed when scrolling the collection, i.e. when cells are reused. The problem goes away if I explicitly mark the subview as needing to be redrawn like so:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// ...
assert(cell.dot.superview == cell.contentView)
cell.dot.setNeedsDisplay() // apparently required for dot to be redrawn
return cell
}
Is it normal behavior that subviews of reused UICollectionViewCells are not automatically redrawn? Am I missing something else?

UITableView with fading cells on top and bottom - swift

I need my table view to fade its top and bottom cells as it is scrolled. I have tried some solutions involving gradient and masks but non of it worked, the gradient from clear to white has a black tint. Does anyone has a solution to accomplish that in swift?
You can achieve desired effect by using some methods defined in UITableViewDelegate protocol. First thing you need to know that cell main subview is contentView add all other subviews are subviews of it. What you need to do is to set contentView alpha to 0 in cellForRowAtIndexPath:indexPath: method.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = "Fade Cell"
cell.contentView.alpha = 0 // Here we set the alpha of the content view
return cell
}
If you run your application now, you would have plain white cells. Only thing we need now is to know when cells are displayed, so we can show contentView. Second UITableViewDelegate protocol method comes in handy now.
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
UIView.animateWithDuration(0.4) {
cell.contentView.alpha = 1
}
}
This delegate method is called when cells are preparing to be displayed, and it's the perfect place to animate contentView alpha property to 1.

Resources