4

I have a UIView Subclass which presents three buttons arranged with two above one. I use a StackView to arrange the top two buttons, and the bottom arranged view is just a single button. The problem is that the button added as an arrangedSubview to the root StackView receives clicks, while the buttons inside of the StackView in the StackView do not.

(note: dependencies should not be affecting anything. DynamicButton is just a cocoapod UIButton subclass. InsetView is my own UIView subclass that embeds the buttons in a view with constraints. And pinTo is just a UIView extension that adds pinning constraints to the view. These things shouldn't be the bug given that the bottom button receives clicks and is under 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")
    }
}
Alex Bollbach
  • 4,370
  • 9
  • 32
  • 80

0 Answers0