I have 3 cells, first cell has label, second cell has textfield, and the last cell has button, by using this it give automatic size but how to remove space between cells
if let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowlayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}
or how to use this method in my scenario
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
code
}
Use the following collection view delegate functions:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
And for spacing to the sides:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: minimumInteritemSpacingForSectionAtIndex UICollectionViewLayout, section: Int) -> CGFloat {
return 0
}
Swift 4 & 5
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
Also check out the documentation
Swift 5
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
If you have an init function for your controller, you can avoid bulking it with the minimumLineSpacing & minimumInteritemSpacing methods and do the following:
init() {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
super.init(collectionViewLayout: layout)
}
See: https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.minimumInteritemSpacing = 0 // The minimum spacing to use between items in the same row.
myCollectionView.layout = layout // or myCollectionView.collectionViewLayout ( I don't have compiler here so I do not know the property name, but I know it has property that has layout in its name )
Swift 4.0
There are 2 delegate method of UICollectionViewDelegateFlowLayout to remove space between item and section.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
Related
I want to have 3 cells in one row and I am only able to place 2 cells in the collection view
Modify minimumInteritemSpacingForSectionAt and minimumLineSpacingForSectionAt method of UICollectionViewDelegateFlowLayout to smaller value:
var reducedPadding: CGFloat = 8
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return reducedPadding
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return reducedPadding
}
I have a UICollectionview of size height 40. UICollectionviewcell of (25,25). Cells are not getting shown. every delegate method call is working fine.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 25.0, height: 25.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
class yourviewcontroller:UICollectionViewDelegate ,UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout{
}
UICollectionViewDelegateFlowLayout add this
I'm working in Swift and I want my cells to look similar to how it does on this picture on an iPhone 8 plus
But when I load it on a iPad, the size of the cells do not adjust to the screen size. It looks like this:
Does anyone know how I can do this in my CollectionViewController?
Try this.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let yourWidth = (collectionView.bounds.width) / 10.0
let yourHeight = yourWidth
return CGSize(width: yourWidth, height: yourHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
There is a delegate Method in UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let collectionViewCellSize = collectionView.frame.size.width / 10 // to show 10 cells in a row
return CGSize(width: collectionViewCellSize, height: collectionViewCellSize)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == self.collectionViewVideo {
var collectionViewSize = collectionViewVideo.frame.size
collectionViewSize.width = collectionViewSize.width/3.0 //Display Three elements in a row.
return collectionViewSize
} else {
return CGSize(width: 60, height: 60)
}
}
You also need to take space in account.
Space is your collection view item space
Following is the code has two cells and 20 spaces.
(I am not writing exactly code you need You should do it by your self)
extension YourViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 20
}
//--------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 20;
}
//--------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: ( self.collectionView.frame.size.width - 60 ) / 2,height:( self.collectionView.frame.size.width - 60 ) / 2)
}
//--------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 20, 20, 20)
}
}
Here 60 means SPACE 20 CELL SPACE 20 CELL SPACE 20
I am currently working in creating a custom calendar where I tried to display Seven cells in a row , But unfortunately some lines occurs randomly can any one guide the error.I have attached the screen shot
I have attached the sample code:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let s = CGSize(width: CGFloat(UIScreen.main.bounds.size.width / 7), height: CGFloat(UIScreen.main.bounds.size.height / 7))
return s
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0,0,0,0)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numDays+weekday
}
You can manage it by UICollectionViewDelegateFlowLayout methods
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0 // OR change as you need
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0 // OR change as you need
}
This behaviour appears when you compute the size of your cell.
Hear, you are doing :
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let s = CGSize(width: CGFloat(UIScreen.main.bounds.size.width / 7), height: CGFloat(UIScreen.main.bounds.size.height / 7))
return s
}
The cell's size may not be rounded, therefor the runtime tries to do its best to fill the collection view with your constraints but end up with these little flaws.