CollectionView. Maximum distance between Item. Swift 3 - ios

I know there is only minimum spacing between items and you can achieve that via size inspector or
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
Indeed on the simulator on SE my items are shown like in the image below.
But when I run on a larger simulator my distance between cell items is increasing and aren t showing as expected.
Is there any method to set like a maximum size between them ?

Logically, there is another factor that you should keep in mind for setting the space between the items in your collection view, which is the size of the cell. If you are getting the cell as is (with its default size) -and my assumption is that's your case-, it would be statically dequeued in the collection view, i.e the size of the cell would be as is.
So, what you should do is to tell what is the desired size (width) of the cells to be displayed in the collection view, by conforming to
UICollectionViewDelegateFlowLayout and implementing collectionView(_:layout:sizeForItemAt:):
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// let's assume that each row in the collection view should contains 3 cells:
let desiredWidth = collectionView.frame.width / 3
// height is also equals to the width to be a square...
return CGSize(width: desiredWidth, height: desiredWidth)
}
At this point if you are setting the minimum space between items to zero, each row should contains three cells with no space between them.
If you are aiming to set a little space between the cells, you should reflect it when declaring the desired width for the cell, for example:
// since each row contains 3 cells, subtracting 2 from each cell width
// means it would be 6 points (3 * 2) divided at the whole row (2 spaces, 3 points for each)
let desiredWidth = collectionView.frame.width / 3 - 2

Related

UICollectionView Cell not filling entire screen (to bottom)?

UICollectionView cell isn't filling the entire screen. When scrolling, it then messes up (leaving spacing on bottom because the image is too short.
Things I've tried:
-Collection View Inset is "None"
-Min spacing is 0 for cells, 0 for lines
-Image inside cell is constraint to 0 on all four sides
-UI collection View is constraint to 0 on all four sides to view controller
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Even you can set the cell to uicollectionview size
let cvRect = collectionView.frame
return CGSize(width: cvRect.width, height: cvRect.height)
}
Storyboard is made with iPhone 12 Pro max. Simulator: iPhone 12 Pro. Each scroll has a bigger image from the previous.

Collection view every next cell should visible 10% in the current screen

I have many collection view cells. I want to show two collection view cells on the screen with respect to first cell (A) should visible 90% of the screen and second cell (B) should visible 10% in the present screen, if i scroll to next cell. first cell (A) visible 10% of the screen and second cell (B) visible 90% of the screen.
I was Achieved by below code.
Perfectly working if i have 2 cells
Not exactly working if i have more than 2 cells.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let value = 0.9;
let width = UIScreen.main.bounds.size.width*CGFloat(value)
return CGSize(width: width, height: collectionView.frame.height)
}
RefImage:
Expected output

CollectionView rows Side By Side - should have same dynamic height

On the IPad I have two different UICollectionViewCells side by side like two columns. The first one have a width of 30% the second of 70%.
This two cells get dynamic content and have different heights. At the moment I return some static height in
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { ... }
This height is always too large.
So I want that the rows have just one height and this height is the height of the row with more content.
Is there a method how I can calculate how much height a UICollectionViewCell needs? If not, which possibilities do I have?
I tried to generate the height by content like this
for item in contentInARow {
contentHeight += 40
}
But this 40 is too static because the content could be too big or small.
I'm thankful for some input.

Swift iOS -How to Truncate Horizontal CollectionView Cells when they are about to go past Screen Bounds

I have a collectionView cell that displays the cells horizontally. I only want a set number of cells to show on the screen and after the amount I want to display ... or something to signify there are more cells available. I don't want the user to scroll to more cells
Because there are different screen sizes like iPhone 4/5/6/7/iPad etc.. I don't want to put an exact number of items to get displayed. Instead I want to to truncate the amount of items based on the screen size for eg. On an iPhone 7+ in .compact size class at least five cells can fit on the screen but on an iPhone 4 only three cells can fit on a screen and on an iPad probably 20 cells.
I imagine I'd do something like this:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//I know this is comparing an Int to a CGRect and it won't work
if items.count > UIScreen.main.bounds.width{
//stop count and display truncation dots ...
}
return self.items.count
}
How would I accomplish this?
To accomplish that you need to implement the protocol method
collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
You need to manually return the size of each cell so in this case you will calculate the width of the cell depending on your screen size.

SWIFT - Collection View Cell size

I have an app that uses photos from web and put them into the collection view.
in collection view i have 3 rows of cells 1:1 size which calculates from screen width / 3.
every thing is working good but there is a thing, on for ex. iphone 6s+ the cells are all tightly get together with no spacings at all. but on iphone 5s i getting some spacing between cells, in only vertical way as on the screenshot.
iPhone 6s+ Screenshot
iPhone 5s Screenshot
there is some code:
let screenSize: CGRect = UIScreen.mainScreen().bounds
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: screenSize.width / 3, height: screenSize.width / 3)
}
i have also checked if it is an image view problem but it is not.
What can i do to remove those spacings?
Creating CollectionView and then fit cells and spacing programmatically, you can try to add minimumInteritemSpacingForSectionAtIndex and minimumLineSpacingForSectionAtIndex on your own code.
func collectionViewLaunch() {
// layout of collectionView
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
// item size
layout.itemSize = CGSizeMake(self.view.frame.width / 3, self.view.frame.size.width / 3)
// direction of scrolling
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
// define frame of collectionView
let frame = CGRectMake(0, 0,
self.view.frame.size.width, self.view.frame.size.height - self.tabBarController!.tabBar.frame.size.height - self.navigationController!.navigationBar.frame.size.height)
// declare collectionView
collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
collectionView.backgroundColor = .whiteColor()
self.view.addSubview(collectionView)
self.collectionView.hidden = false
// define cell for collection view
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
// call function to load posts
loadPosts()
}
// cell line spacing
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
// cell inter spacing
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
// cell numb
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return picArray.count
}
Hope help you.
Do the original sum, round it down and multiply back by the number of columns.
Adjust the frame width of the collectView to this value and then cell widths will always fit the view perfectly.
Since the iPhone 5s' width is 640, dividing it by 3 would result in 213,33333333. Since iOS doesn't like those values, it will correct the value to 213, which will create a spacing when having three of those next to each other, since 213*3 does not equal 640. On the iPhone 6s Plus, the width is 1080 which is equally dividable by 3, resulting in 360, Here, no spacing will occur.
Try finding a divider which equals a natural number for any screen size and the spaces should be gone, or you try to find another solution.
I had a similar issue with horizontal and vertical collection views.
I was using a slider to set the number of columns and then resize the UIImages in the collection accordingly to the new cell size
On top of this I cut an image of an arrow in two and placed each half at the sides of the collection view so that when it displayed it had the result of drawing a full arrow in between images... I couldn't be bothered with custom seperator inserts at the time...which is where the arrow "should" have lived.
Hence every now and then I had a pixel wide gap in the arrows depending on screensize and orientation.
It IS all in the rounding up of the division result.
You have to find a width/height that divides as best you can to fit the screen most used.
You can apply some conditional resizing of the views if the values from the sums are not integers but finding the correct value to replace it with meant having to specify every eventuality.
In the end I gave up and "lived" with the single-pixel-wide gap

Resources