If the application crashes due to fatal error: unexpectedly found nil while unwrapping an Optional value - you may need to add some delay before adding a gesture recognizer to the stack view.
If you access the stack view property in a subclass of the collection view cell or in the methods of the collection view delegate, the stack view may not yet be initialized. Try using a timer or GCD to add a 0.1 second delay and it should work fine ...
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(prepareStackView), userInfo: nil, repeats: false)
...
@objc func prepareStackView() { let tap = UITapGestureRecognizer(target: self, action: #selector(stackViewTapped)) myStackView.addGestureRecognizer(tap) }
source share