Reputation: 31
I wrote a code that I got collectionview with 4 cells and I can swipe to see each one of them.
My collectionview backgroundcolor is red and in my cell I put an image (before that I put blue color instead of an image) and I got a problem that I see little red line form the collection view in my cell.
I must say that I had problem that I had bigger line before that and I fix that by hidden navigation view of my viewcontroller.
class CheckViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .red
cv.dataSource = self
cv.delegate = self
cv.isPagingEnabled = true
return cv
}()
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)
view.addSubview(collectionView)
// collectionView.frame = view.frame
//use autolayout instead
collectionView.anchorToTop(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
// func numberOfSections(in collectionView: UICollectionView) -> Int {
// return 4
// }
//
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
// cell.backgroundColor = .white
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
extension UIView {
func anchorToTop(top: NSLayoutYAxisAnchor? = nil, left:
NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right:
NSLayoutXAxisAnchor? = nil) {
anchorWithConstantsToTop(top: top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
}
func anchorWithConstantsToTop(top: NSLayoutYAxisAnchor? = nil, left:
NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right:
NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0,leftConstant:
CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0)
{
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: topConstant).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: bottomConstant).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: leftConstant).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: rightConstant).isActive = true
}
}
}
And this is the image.
Upvotes: 1
Views: 796
Reputation: 2253
There are two problems
1) You are setting the cell's size relative to the view rather than the collectionView.
Your error in setting the cell's size..
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
2) It looks like your backgroundImage for the view doesn't fit 100%. You want to use another aspect ratio to fill the view. You probably want to use .scaleToFill.
Difference between UIViewContentModeScaleAspectFit and UIViewContentModeScaleToFill?
Upvotes: 1