0

I am trying to setup a Carousel-like UI with buttons in each carousel cell. I am using a Collection View to do this. The buttons show up in the cell, but do not seem to respond to the touch up inside event which should run tapped() and return "TAPPED!". Here is the code for the Collection View Cell which includes the button. Thanks for the help!

CarouselCollectionViewCell.swift

import UIKit
import Foundation

class CarouselCollectionViewCell: UICollectionViewCell {

    static let identifier = "CarouselCollectionViewCell"

    @objc func tapped() {
      print("TAPPED!")
    }

    var mainView : UIView = {
      var mainCellView = UIView()
      mainCellView.translatesAutoresizingMaskIntoConstraints = false
      return mainCellView
    }()

    var remindButton : UIButton = {
      var textView = UIButton()
      textView.translatesAutoresizingMaskIntoConstraints = false
      return textView
    }()


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

      self.addSubview(mainView)
      mainView.addSubview(remindButton)
      mainView.backgroundColor = .white

      //Style
      mainView.layer.cornerRadius = 8
      mainView.layer.masksToBounds = true

      // Constraints
      mainView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
      mainView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
      mainView.heightAnchor.constraint(equalToConstant: 360).isActive = true
      mainView.widthAnchor.constraint(equalToConstant: 290).isActive = true

      self.remindButton.backgroundColor = UIColor(red: 69.0/255.0, green: 198.0/255.0, blue: 255.0/255.0, alpha: 1.0)
      self.remindButton.layer.cornerRadius = 20
      self.remindButton.layer.masksToBounds = true
      self.remindButton.bottomAnchor.constraint(equalTo: self.mainView.bottomAnchor, constant: -30).isActive = true
      self.remindButton.centerXAnchor.constraint(equalTo: self.mainView.centerXAnchor).isActive = true
      self.remindButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
      self.remindButton.widthAnchor.constraint(equalToConstant: 175).isActive = true
      self.remindButton.setTitle("Add Reminder", for: .normal)
      self.remindButton.addTarget(self, action: #selector(self.tapped), for: .touchUpInside)
    }

  override func layoutSubviews() {
    super.layoutSubviews()
  }

    required init?(coder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }
}
aritroper
  • 1,549
  • 5
  • 14
  • 19
  • Make sure the `collectionView` `cell` height and width are correct to contain the calculated height and width of `mainView` – Manee.O.H Jun 08 '20 at 19:55
  • I tried adding "self.heightAnchor.constraint(equalToConstant: 360).isActive = true" and "self.widthAnchor.constraint(equalToConstant: 360).isActive = true" to set cell height and width but to no avail – aritroper Jun 08 '20 at 20:17
  • Try to change cell height and width using `collectionView` itself, and conform to `UICollectionViewDelegateFlowLayout` refere to this question https://stackoverflow.com/questions/38028013/how-to-set-uicollectionviewcell-width-and-height-programmatically – Manee.O.H Jun 08 '20 at 20:44

0 Answers0