I am using this code to get correct type but not getting the view what i want can any one tell me where am i wrong
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
screenSize = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
videosCollectionView.delegate = self
videosCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = videosCollectionView.dequeueReusableCell(withReuseIdentifier: "Videos", for: indexPath as IndexPath) as! VideosCollectionViewCell
cell.mainImgVw.image = logoImage[indexPath.row]
cell.durationLabel.text = "duration"
cell.nameLabel.text = "Test Video"
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let xPadding = 10
let spacing = 10
let rightPadding = 10
let width = (CGFloat(UIScreen.main.bounds.size.width) - CGFloat(xPadding + spacing + rightPadding))/2
let height = CGFloat(215)
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(10, 10, 10, 10)
}
and what i am getting
Please tell me where i am wrong. Please help me.
My storyboard is like this
As you have told that you want to use collectionViewFlowDelegateLayout. So, you have to set all values 0 from storyboard as I have shown below.
Then you have to write this code in your viewcontroller class.
This method is used for set the cell size in collection view.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let xPadding = 10
let spacing = 10
let rightPadding = 10
let width = (CGFloat(UIScreen.main.bounds.size.width) - CGFloat(xPadding + spacing + rightPadding))/2
let height = CGFloat(215)
return CGSize(width: width, height: height)
}
This method is used for the margins to apply to content in the specified section.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(10, 10, 10, 10)
}
This method is used for spacing between successive rows or columns of a section.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
Your code works fine and there isn't any problem, check Min Spacing
in Size Inspector
tab it should be too large, set it 10
Try this :-
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (self.view.frame.width - 8) / 3.0 , height: (self.view.frame.width - 8) / 3.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(8, 8, 0, 8)
}
Hope it will help you
Declare UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
Set min space for cell and for line from storyboard like this.
Set collectionview cell width like this,
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var cellWidth:CGFloat = (collectionView.frame.size.width - 20) / 2;
return CGSize(width: cellWidth, height: 215);
}