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.
Related
I need to create a UIcollectionview with grids of cells with 3 cells per row. After a certain amount of cells, lets say 60, I need to display a big cell with image view inside of it, and the cells needs to be in full width of the UIcollectionview.
I have done created the 3 cells part, but I failed to figure out how to insert the big image cell and how to layout it properly.
Thanks.
How about you do a few sections? And the header is the other cell. You can put an image there as well.
You could specify the size of the cell in the UICollectionViewFlowLayout delegate method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//logic to find if the cell is 60 th item and then and dont forget to return the size of the other cells.
if 60th cell {
return size
} else {
return size
}
}
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
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.
I am building a collection view, every cell has a fixed width but a varying height. There should be a border of 1 px within each cell. I have tried to find a way to make them vertically align but end up with white space in between them. Also some times the cells end up on top of each other.
What would be the best wat to solve this problem. I have considered subclassing UICollectionViewFlowLayout
I am currently setting the size like this:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: CGFloat(defaultCellSize), height: CGFloat(defaultCellSize * ratio))
}
Try using this library https://github.com/chiahsien/CHTCollectionViewWaterfallLayout It has handled what is required.
I have a collection view with the minimum spacing set to 10 for cells and lines. The horizontal space between the cells adjusts correctly, but I don't know how to alter the vertical spacing between the rows of cells. On the 6, which it was originally developed on, the space is negligible and works fine. But on every other device, the space quadruples in size for no apparent reason. Is there any way to set the spacing between the rows of cells to be a constant value across all devices?
Right now there isn't a way to set the size of collection view cells using AutoLayout. My guess is that the size of your cells just doesn't look good on other screen sizes, so you most likely will need to implement the UICollectionViewDelegateFlowLayout protocol (which extends the UICollectionViewDelegate protocol) and the func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize method.
Although if that's not the case maybe posting some screenshots would help.
Example method:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if(self.view.bounds.width > 400){
return CGSize(width:self.view.bounds.width/3-10, height:self.view.bounds.width/3-10)
}
return CGSize(width:self.view.bounds.width/2-10, height:self.view.bounds.width/2-10)
}