I have setup my collection view in a way, that my first image comes full size to the right and half the size of screen vertically with the following code
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.row == 0 {
return CGSize(width: self.collectionView.frame.width, height: ( self.collectionView.frame.height / 2 ))
}
let width = self.collectionView.frame.width / 3 - 1
return CGSize(width: width, height: width)
}
it works ok. But when collection view has only 2 item then the second item comes in the middle rather that on the left.
How to fix this ??
Related
So I have UICollectionView on my Controller
UICollection view consist of two sections.
So when I start scrolling, it changes cell sizes
My code for layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let section = indexPath.section
let row = indexPath.row
if section == 0 {
if (row == 0){
let width = view.frame.width
return.init(width: width, height: 120)
}else{
let width = view.frame.width
return.init(width: width, height: 50)
}
}else{
//
//return.init(width: 200, height: 120)
let width = collectionView.bounds.width / 2 - 10
return.init(width: width, height: 253)
}
}
Producs are in second section, but it looks like it gives a width from first section
Make sure estimateSize is set to NONE in storyboard.
Your error the first size for section 0 but anther section you changeded the cell size you can use this code :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width / 2 - 10
let height = collectionView.bounds.height
return.init(width: width, height: height)
}
How to create like this collection view with different cell size inside table view cell and also inside collection view cell image need to download from url cell
i tried below code, but last 2 small cells going up , and also when the image downloaded from url collection view is not loading until manually touch collection view
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cVWidth = collectionView.frame.width
let biggerCellWidth = (cVWidth / 2.5) - 5
if indexPath.row == 2 || indexPath.row == 3 {
let testValue:CGFloat = 3
return CGSize(width: (biggerCellWidth / 2) - testValue , height: (biggerCellWidth / 2) - testValue )
}
return CGSize(width: biggerCellWidth, height: biggerCellWidth)
}
For creating a collection view inside a table view go to this link
There is a method in UICollectionViewDelegateFlowLayout
collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
where you can set the size of collection view cell size. Say you want to set the third and fourth cell's height as half of the collection view and square in size. Put this condition
if indexPath.row == 2 || indexPath.row == 3
{
let height = collectionView.bounds.size.height / 2
return CGSize(width: height, height: height)
}
I have a UICollectionView and I have implemented the delegate to calculate the width of my cells for 0 spacing between cells.
It works great on its own, but when I have it inside a container view less than the size of the device, iOS incorrectly works out the spacing between the cells adding a horizontal space I don't want.
I have verified the width I am using to calculate the cell size is correct, so I'm not sure what is causing the problem.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let widthPerItem = view.frame.width / itemsPerRow
return CGSize(width: widthPerItem, height: 60)
}
You are calculating your cell size based upon the size of the view. Since the collectionView doesn't take up the full width of the screen, you are getting the incorrect value. Instead, you should base your calculation on the width of the collectionView itself.
Use the bounds of the collectionView instead of the frame of the view:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let widthPerItem = collectionView.bounds.width / itemsPerRow
return CGSize(width: widthPerItem, height: 60)
}
Ok, the problem was actually related to my uicollectionviewcontroller not correctly sizing to it's parent container view. Changing to a uiviewcontroller with an embedded uicollectionview plus constraints fixed the problem.
I'm using this method below in my UICollectionView to stretch/fill the cell, but it seems like it's constraining to the margins. It's stopping like 10 px shy of the edges. I basically want to set the constraints of the cell to "0" and "0" to fill the width completely and NOT constrained to the margins.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let customHeight = CGFloat(116)
return CGSize(width: collectionView.frame.size.width, height: customHeight)
I have a UICollectionView that displays two kind of cells according to whether the item in its datasource is a "folder" or a "photo"
It appears that when the UICollectionView is populated with both type of cells and when there are less than 3 number of "photo" cells to show per row, the cells are not being left-aligned one next to each other but rather try to fill out the whole empty space of the row.
Preview 1:
Preview 2
However when the exact UITableViewCollection displays only "photos" cells, it seems to be behaving normally:
Here are the delegate methods that handle the cell sizes and spacing:
extension DropboxBrowserViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
{
let folderTypeCellHeight = CGFloat(64.0)
let collectionViewWidth = collectionView.bounds.size.width
let numOfPhotosPerRow = CGFloat(3.0)
let layout = collectionViewLayout as! UICollectionViewFlowLayout
let insets = (layout.sectionInset.left, layout.sectionInset.right)
// Go full width if the cell displayed a folder name
if let _ = listingsDataSource?.listings[indexPath.row] as? Files.FolderMetadata {
return CGSize(width: collectionViewWidth, height: folderTypeCellHeight)
}
// return size to fit `numOfPhotos` photos per row
else {
let cellSpacing = SavePhotosCollectionViewCell.cellSpacing
let cellSize = (collectionViewWidth - insets.0 - insets.1 - (CGFloat((numOfPhotosPerRow - 1)) * cellSpacing)) / numOfPhotosPerRow
return CGSize(width: cellSize, height: cellSize)
}
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
return 2.0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
Any ideas on why this might be happening?
Thanks
EDIT
The storyboard looks like this. I don't why the "photos" cell is being displayed in the middle over there and if this has something to do with the issue.