1

I am trying to follow the tutorial on this post: How to implement UITableView`s swipe to delete for UICollectionView exactly like it is, except for adding outlets from the storyboard, I have initialized everything (including my collection view) programmatically.

When I reach the screen containing the collection view, the swiping action to reveal the delete button under it does not take place. I have set the delegate method shouldRecognizeSimultaneouslyWith to return true and have set up the cell like so:

import UIKit

class CustomCollectionViewCell: UICollectionViewCell, UIGestureRecognizerDelegate {

    // Cell data
    var aLabel       : UILabel!
    var bLabel       : UILabel!
    var myImageView  : UIImageView!
    var myContentView: UIView!

    // Swipe to delete cell
    var contentViewLeftConstraint             : NSLayoutConstraint!
    var contentViewRightConstraint            : NSLayoutConstraint!
    var deleteButton                          : UIButton!
    var startPoint                            = CGPoint()
    var startingRightLayoutConstraintConstant = CGFloat()
    var swipeView                             : UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        backgroundColor = .clear

        myImageView = UIImageView(frame: CGRect(
            x     : 15,
            y     : 10,
            width : frame.height - 20,
            height: frame.height - 20
        ))

        aLabel = UILabel(frame: CGRect(
            x     : myImageView.frame.maxX + 15,
            y     : 15,
            width : frame.width - 20 - myImageView.frame.maxX,
            height: 23
        ))

        bLabel = UILabel(frame: CGRect(
            x     : myImageView.frame.maxX + 15,
            y     : aLabel.frame.maxY,
            width : frame.width - 20 - myImageView.frame.maxX,
            height: 30
        ))

        swipeView = UIView(frame: CGRect(
            x     : contentView.frame.width - 60,
            y     : contentView.frame.minY,
            width : 60,
            height: contentView.frame.height
        ))
        deleteButton = UIButton(frame: swipeView.frame)
        deleteButton.backgroundColor = .red  // For visibility purposes
        swipeView.addSubview(deleteButton)

        myContentView = UIView(frame: contentView.frame)
        myContentView.addSubview(myImageView)
        myContentView.addSubview(aLabel)
        myContentView.addSubview(bLabel)

        contentView.addSubview(swipeView)
        contentView.addSubview(myContentView)

        // Pin the song data content view's left margin to the cell content view's left margin.
        contentViewLeftConstraint  = NSLayoutConstraint(
            item      : myContentView,
            attribute : .left,
            relatedBy : .equal,
            toItem    : contentView,
            attribute : .leftMargin,
            multiplier: 1.0,
            constant  : 0.0
        )
        contentViewLeftConstraint.isActive = true

        // Pin the song data content view's right margin to the cell content view's right margin.
        contentViewRightConstraint = NSLayoutConstraint(
            item      : myContentView,
            attribute : .right,
            relatedBy : .equal,
            toItem    : contentView,
            attribute : .rightMargin,
            multiplier: 1.0,
            constant  : 0.0
        )
        contentViewRightConstraint.isActive = true
    }
}

My ViewController methods are the exact same as the ones in the tutorial. I printed out the pan movement like it is done in Ray Wenderlich's original tutorial and the pan gesture is behaving as expected. It's just that myContentView won't slide leftward to reveal the delete button. Maybe I am setting the constraints incorrectly?

Please help!

0 Answers0