How to show the correct cell in a UICollectionView on changing device orientation? - uicollectionview

I am making a Gallery, just like the photos app. I am using a UICollectionView to show images. These images can be both landscape and portrait. I'm setting the cell size as follows:
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.frame.size
}
Also I'm setting:
collectionView.isPagingEnabled = true
I am getting the behaviour as expected in both portrait and landscape orientations of the device.
The problem I am facing is when I change the orientation, wrong cell is displayed.
I tried changing the target offset using:
func collectionView(_ collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint
This did not work for me. How do I fix this?

Related

UICollectionViewCell doesn't fit on screen

Does anyone know why this would happen or how to fix it? I am trying to make a simple collection view but for some reason, the cells have a little bit of an offset on the x-axis.
It's supposed to look like this
But it comes out like this
Here's what I have
func collectionView(_ collectionView:UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenWidth = UIScreen.main.bounds.width
return CGSize(width: screenWidth, height:135.0)
}
Okay well to solve the issue I just created a brand new view controller in the Storyboard, It was a pain but I honestly could not figure out what was wrong. I think that there must have just been a glitch somewhere in the constraints maybe, but even after I deleted all the constraints I still had the problem. Maybe I'm just the big dumb.

flowLayout.itemSize and flowLayout.minimumInteritemSpacing

I set the cell size in
collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath).
However, in other callbacks of the collectionView I see that the flowLayout.itemSize is not the size I set, despite the size being correctly rendered on screen. Why is this not the case?
Separate but related-how/where do I set the flowLayout.minimumInteritemSpacing?
So my answer might be kind of hack-ish but it works perfectly for me.
In
collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath)
before I return I set the flowLayout.itemSize and minimumInteritemSpacing (in my case 1/6 the item size). Now whenever I call these I get the desired values.

How can I horizontally center rows in CollectionView with sections

So I know this topic has been tackled in the past, still, I have try'd applying all previous suggestions but non have actually solved my issue.
I have a collectionView that contains a dynamic amount of sections and items and I need to constraint all rows to be horizontally centered.
I have try'd to apply a pod called KTCenterFlowLayout but with no success since apparently it doesn't work well with a multi section collectionView.
Is there anyway to center collectionView rows in a multi sectional CollectionView in swift4?
'func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenWidth = (UIScreen.main.bounds.size.width-4) / 3
return CGSize(width:screenWidth , height: screenWidth);
}'
Write it down that in your View Controller and set that you whatever thing that you want..enter image description here

UICollectionView fails to honour zPosition and zIndex - iOS 11 only

I try to find a workaround for this radar: http://www.openradar.me/34308893
Currently in my - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes I'm doing the following:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_MSEC * 50), dispatch_get_main_queue(), ^{
self.layer.zPosition = 0;
});
But no matter how many NSEC_PER_MSEC I enter it doesn't work quite right, the scrolling indicator sometimes hides behind the headers, especially when ne headers appear. Is there another/better approach?
I had a similar problem where a header view in a UICollectionView was getting rendered above another view that had it's zIndex set > 0 in a custom UICollectionViewFlowLayout.
The answer for me was to set the header view layer's zPosition in willDisplaySupplementaryView:
override public func collectionView(_ collectionView: UICollectionView,
willDisplaySupplementaryView view: UICollectionReusableView,
forElementKind elementKind: String,
at indexPath: IndexPath) {
if elementKind == UICollectionView.elementKindSectionHeader && type(of: view) == CollectionViewSectionHeaderView.self {
view.layer.zPosition = -1
}
}
iOS11 doesn't seem to honor z-indexes in UICollectionViewFlowLayout subclasses.

Hide grey color when cell is touched

I'm making an app with a multiple cell selection (I use checkmark) like this
The problem is that when I click on a cell, it remains gray and is really bad. How can I fix it?
If you do not want to show grey selection at all:
Select the UITableViewCell in Storyboard
In Inspector, set selected to none
You can find more options to set it programatically in this answer.
try this code at didSelectRowAt#
cell.selectionStyle = UITableViewCellSelectionStyleNone
You can use this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
or
cell.selectionStyle = UITableViewCellSelectionStyle.None