0

I am setting up a UiCollectionView and I am trying to have the view scroll horizontally. The View is setup up and I can see labels and images but it does not scroll at all. The height of the collectionViewCell itself is 350 and how i am setting up the cell is

let SubView: UIView = {
     let view = UIView()
     view.backgroundColor = GREEN_Theme
     view.layer.cornerRadius = 10
     return view
 }()
 
 let headerLabel: UILabel = {
     let label = UILabel()
     label.text = "label"
     label.font = UIFont.systemFont(ofSize: 30)
     label.font = .boldSystemFont(ofSize: 30)
     label.numberOfLines = 0
     label.textColor = UIColor.black
     return label
 }()


 override init(frame: CGRect) {
     super.init(frame: frame)
    setupViews()

   addSubview(SubView)
     SubView.anchors(top: topAnchor, topPad: 0, bottom: nil, bottomPad: 0, left: leftAnchor, leftPad: 0, right: rightAnchor, rightPad: 0, height: 1, width: self.bounds.width - 20)
    
   addSubview(headerLabel)
   headerLabel.anchors(top: SubView.bottomAnchor, topPad: 5 , bottom: nil, bottomPad: 0, left: safeAreaLayoutGuide.leftAnchor, leftPad: 15, right: nil, rightPad: 0, height: 50, width: 200)


  
 }

 required init?(coder aDecoder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
 }

let recentCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.clear
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    
    
    return collectionView
}()

func setupViews() {
    
    backgroundColor = UIColor.clear
    
    addSubview(recentCollectionView)
    contentView.isUserInteractionEnabled = true
    recentCollectionView.dataSource = self
    recentCollectionView.delegate = self
    recentCollectionView.isUserInteractionEnabled = true
    recentCollectionView.backgroundColor = UIColor.blue
    recentCollectionView.register(recentCell.self, forCellWithReuseIdentifier: cellID)
    recentCollectionView.layer.zPosition = 1
    recentCollectionView.topAnchor.constraint(equalTo: topAnchor, constant: 40).isActive = true
    recentCollectionView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 5).isActive = true
    recentCollectionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -5).isActive = true
    recentCollectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -40).isActive = true
    
 
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return reviews.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! recentCell
    cell.configureCell(reviews[indexPath.row])
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     return CGSize(width: 100, height: frame.height - 5)
 }


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
      return UIEdgeInsets(top: 100, left: 14, bottom: 0, right: 14)
  }

}


 class recentCell: UICollectionViewCell {

override init(frame: CGRect) {
      super.init(frame: frame)
    setupViews()
    
  }
    
let imageView: UIImageView = {
    let iv = UIImageView()
    iv.image = UIImage(named: "rake")
    iv.contentMode = .scaleAspectFill
    iv.layer.cornerRadius = 16
    iv.layer.masksToBounds = true
    return iv
}()

let nameLabel: UILabel = {
    let label = UILabel()
    label.text = "Zach Wilcox"
    label.font = UIFont.systemFont(ofSize: 14)
    label.numberOfLines = 2
    return label
}()

let categoryLabel: UILabel = {
    let label = UILabel()
    label.text = "Yard Work"
    label.font = UIFont.systemFont(ofSize: 13)
    label.textColor = UIColor.darkGray
    return label
}()

let starControls = starStackView()
     
 required init?(coder aDecoder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
 }


func setupViews() {
    
    addSubview(imageView)
    addSubview(nameLabel)
    addSubview(categoryLabel)
    addSubview(starControls)
    
    starControls.translatesAutoresizingMaskIntoConstraints = false
    starControls.distribution = .fillEqually
    
    imageView.frame = CGRect(x:0, y: 0, width: frame.width, height: frame.width)
    nameLabel.anchors(top: imageView.bottomAnchor, topPad: 1, bottom: nil, bottomPad: 0, left: imageView.leftAnchor, leftPad: 0, right: nil, rightPad: 0, height: 20, width: 100)
    categoryLabel.anchors(top: nameLabel.bottomAnchor, topPad: 1, bottom: nil, bottomPad: 0, left: imageView.leftAnchor, leftPad: 0, right: nil, rightPad: 0, height: 20, width: 100)
    starControls.anchors(top: categoryLabel.bottomAnchor, topPad: 1, bottom: nil, bottomPad: 0, left: imageView.leftAnchor, leftPad: 0, right: nil, rightPad: 0, height: 20, width: 100)
      }

In my console, I see

the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

I have tried to set the background colors to blue in the recentCollectionView and the collectionView itself to see if the if the height of the cell is exceeding the height of the collectionView but it is not.

images below,

the first image, is the size of the entire contentView.

image1

and this image is the size of the recentCollectionView

image2

as we can see with the images, the recentCollectionView is not larger than the collectionView. Why am I seeing this error and why will my collectionViewFlowLayout not scroll?

zach wilcox
  • 45
  • 14

1 Answers1

0

I have found an answer shortly after posting my question but did not want to delete just in case some other newbie had the same problem. All I had to do was add the recentCollectionView to the contentView's subview not simply just addSubview.

contentView.addSubview(recentCollectionView)

super simple fix.

zach wilcox
  • 45
  • 14