How can I horizontally center rows in CollectionView with sections - uicollectionview

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

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 to show the correct cell in a UICollectionView on changing device orientation?

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?

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.

UITableViewAutomaticDimension with imageview

I have a tableview that contains a custom cell. The custom cell has an imageview and a label. I want to resize the cell based on the text in the label because the imageview dimensions are constant. The imageview takes up the majority of the cell being centered and the label is below it.
self->tableView.rowHeight = UITableViewAutomaticDimension;
I am using this line of code to do so, and I have also set up constraints on the storyboard. When the code runs the image does not show, only the label and the cell is not even resized based off of the text. When I set the view to a default height, both are shown. I am unsure why this is not working. My only thought is that my constraints are incorrect. I have successfully used this with 2 labels but not an imageview. Any help would be appreciated. I would show a picture regarding my constraints, but I can not due to my status.
If you have layout like this of your tableViewCell then set your constraints as shown in below.
You also need to set estimatedRowHeight of your tableView.
self.tableView.estimatedRowHeight = 200;
self.tableView.rowHeight = UITableViewAutomaticDimension;
Remove height constraint form label if set and set label noOflines = 0. It will help you.
Make you set the following
-label numberOfLines to 0
-don't set height for label
-in the bottom contraint between the label and the cell contentView, set it to greater than or equal to e.g 5
Then call the methods below
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}