Button inside collection view cell not responding to layout changes
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a collection view cell with a button inside the cell. When I tap the cell (using .touchUpInside), I don't see the layout changes that are supposed to occur.

Here's the action method that gets called. I know that it is appropriately getting called because I get the log button tapped touch down.

My collection view is using a compositional layout and a UIDiffableDataSource.

    @objc func buttonTapped() {
        print("button tapped touch down")
        self.button.isSpinning = true

    }

The above action sets the property isSpinning in the view, which is supposed to trigger the animation of the activity indicator and the background color change. The SmallCellButton is a class that's a UIView with a transparent button on top of it. It works completely fine when used in other situations than this collection view scenario.

class SmallCellButton : UIView {

    ...

    var isSpinning : Bool = false {
        didSet {
            if isSpinning {
                print("should be spinning")
                self.label.isHidden = true
                self.activityIndicator.startAnimating()
                self.activityIndicator.alpha = 1

                self.layer.borderColor = UIColor.white.withAlphaComponent(0.2).cgColor
                self.layer.backgroundColor = UIColor.black.cgColor
            } else {
                print("should stop spinning")
                self.label.isHidden = false
                self.activityIndicator.alpha = 0
                self.activityIndicator.stopAnimating()


                self.layer.borderColor = UIColor.clear.cgColor
                self.layer.backgroundColor = UIColor.white.withAlphaComponent(0.2).cgColor
            }
        }
    }

}

I get the log of "should be spinning", but I don't ever see the layout changes after the .touchUpInside event. I can step through all the layout code using the debugger, but no visual changes appear.

If I move the layout code from isSpinning to an overrided isHighlighted property, the layout code executes AND visually displays. If I move it to an overrided isSelected property, then it does not visually display, but it does execute.

If I move the layout code from isSpinning to an overrided isHighlighted property, the layout code executes AND visually displays. If I move it to an overrided isSelected property, then it does not visually display, but it does execute.

If I move the layout code from isSpinning to an overrided isHighlighted property, the layout code executes AND visually displays. If I move it to an overrided isSelected property, then it does not visually display, but it does execute.

Also here on stackoverflow:
https://stackoverflow.com/questions/62158098/button-inside-collection-view-cell-not-responding-to-layout-changes

How do I update the layout?

awarded to HeyYehey

Crowdsource coding tasks.

1 Solution

Winning solution

Just try to put the update inside of dispatch

var isSpinning : Bool = false {
didSet {
if isSpinning {
print("should be spinning")
DispatchQueue.main.async { [weak self] in
self.label.isHidden = true
self.activityIndicator.startAnimating()
self.activityIndicator.alpha = 1

                   self.layer.borderColor = UIColor.white.withAlphaComponent(0.2).cgColor
                   self.layer.backgroundColor = UIColor.black.cgColor
            } 
        } else {
            print("should stop spinning")
            DispatchQueue.main.async { [weak self] in
                   self.label.isHidden = false
                   self.activityIndicator.alpha = 0
                   self.activityIndicator.stopAnimating()

                   self.layer.borderColor = UIColor.clear.cgColor
                   self.layer.backgroundColor = UIColor.white.withAlphaComponent(0.2).cgColor
            } 
        }
    }
}
I actually figured out the problem. It had to do with how I was recreating the button and how collection view cells are reused. I tried this solution prior however, ill give it to you for effort :)
iosdev 4 months ago
I actually encountered that kind of problem in objective-c and i tried that solution and it worked like a charm. But that solution is work around for me:)
HeyYehey 4 months ago