Buttons that cannot be clicked when inside a stackview inside a stackview

I have a subclass of UIView that represents three buttons located with two above them. I use StackView to place the top two buttons, and the bottom organized view is just one button. The problem is that the button added as an organized Subview in the root StackView receives clicks, and the buttons inside the StackView in the StackView do not.

(note: dependencies should not affect anything. DynamicButton- this is just a subclass of UIButton cocoapod. InsetView- this is my own subclass of UIView, which introduces buttons into a restricted view. pinTo- this is just an extension of UIView that adds constraints to the view. These things shouldn't be a mistake, given that the bottom button gets clicks and is in the same conditions as the top two buttons.)

class PathContentView: UIView {

    let previousButton: DynamicButton = {
        let r = DynamicButton(style: .caretLeft)
        r.lineWidth           = 3
        r.strokeColor         = .white
        r.highlightStokeColor = .gray
        return r
    }()
    let nextButton: DynamicButton = {
        let r = DynamicButton(style: .caretRight)
        r.lineWidth           = 3
        r.strokeColor         = .white
        r.highlightStokeColor = .gray
        return r
    }()
    let deleteButton: DynamicButton = {
        let r = DynamicButton(style: .close)
        r.lineWidth           = 3
        r.strokeColor         = .white
        r.highlightStokeColor = .gray
        return r
    }()



    init() {
        super.init(frame: .zero)
        setup()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    private func setup() {

        // button components
        let deleteButtonHolder = InsetView(width: 25, height: 25, view: deleteButton)
        let previousButtonHolder = InsetView(width: 25, height: 25, view: previousButton)
        let nextButtonHolder = InsetView(width: 25, height: 25, view: nextButton)

        let rootStackView = UIStackView()
        let topSectionStackView = UIStackView()

        topSectionStackView.distribution = .fillEqually
        topSectionStackView.alignment = .center
        topSectionStackView.spacing = 20
        topSectionStackView.axis = .horizontal

        rootStackView.axis = .vertical
        rootStackView.distribution = .fillEqually
        rootStackView.alignment = .fill
        rootStackView.isLayoutMarginsRelativeArrangement = true
        rootStackView.layoutMargins = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

        addSubview(rootStackView)

        topSectionStackView.addArrangedSubview(previousButtonHolder)
        topSectionStackView.addArrangedSubview(nextButtonHolder)

        rootStackView.addArrangedSubview(deleteButtonHolder)
        rootStackView.addArrangedSubview(topSectionStackView)

        rootStackView.pinTo(superView: self)

        rootStackView.isUserInteractionEnabled = true
        topSectionStackView.isUserInteractionEnabled = true

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
    func tapped() {
        print("tapped")
    }
}
+4
source share

Source: https://habr.com/ru/post/1674393/


All Articles