0

I got a UIStackView ParticipateButtonStackView managing three buttons of the same class: ParticipateButton

These buttons only contain an image. To have some padding at the top and bottom, I set UIEdgeInsets. But this and the .spacing = 1 property seem to not work simultaneously. Why?


Stackview:

class ParticipateButtonStackView: UIStackView {
    //MARK: - Properties & Variables
    var delegate: participateButtonStackViewDelegate?

    //MARK: - GUI Objects
    var buttons: [ParticipateButton]!
    let sureButton = ParticipateButton(type: .sure)
    let maybeButton = ParticipateButton(type: .maybe)
    let nopeButton = ParticipateButton(type: .nope)

    //MARK: - Init & View Loading
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addBackground(color: UIColor.gray)
        buttons = [sureButton, maybeButton, nopeButton]
        setupStackView()
        setupButtons()
    }

    //MARK: - Setup
    func setupStackView() {
        self.axis           = .horizontal
        self.spacing        = 1
        self.alignment      = .leading
        self.distribution   = .fillEqually
    }

    func setupButtons() {
        for button in buttons {
            addArrangedSubview(button)
            button.addTarget(self, action: #selector (self.buttonClick(sender:)), for: .touchUpInside)
        }
    }
}

ParticipateButton

class ParticipateButton: UIButton {

    var typeOfButton: participateLists!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor  = LebensfitSettings.Colors.buttonBG
        self.tintColor        = LebensfitSettings.Colors.basicTintColor
        self.imageView?.contentMode = .scaleAspectFit
        self.contentEdgeInsets = UIEdgeInsets(top: 7, left: 0, bottom: 7, right: 0)
    }

    convenience init(type: participateLists) {
        self.init()
        self.typeOfButton = type
        setImage(type: type)
    }

    func setImage(type: participateLists) {
        var buttonImage: UIImage?
        switch type {
        case .sure:
            buttonImage = UIImage(named: "pb_sure")?.withRenderingMode(.alwaysTemplate)
        [...]
        }
        self.setImage(buttonImage, for: .normal)
    }
}

This is the error that gets thrown:

Will attempt to recover by breaking constraint

NSLayoutConstraint:0x600001adb020 'UISV-spacing' H: [LebensfitFirebase.ParticipateButton:0x7f9899c5eaa0]-(1)-[LebensfitFirebase.ParticipateButton:0x7f9899c5fad0] (active)>

Edit:

participateButtonStackView.leftAnchor = view.leftAnchor
participateButtonStackView.rightAnchor = view.rightAnchor
participateButtonStackView.bottomAnchor = view.bottomAnchor
participateButtonStackView.heightAnchor = equalToConstant(50)
Noodledew
  • 509
  • 4
  • 19
  • What are the other constraints, such as for UIStackView? – Bob Sep 30 '18 at 15:24
  • One view hierarchy above.. Added now. – Noodledew Sep 30 '18 at 16:05
  • Does anything change if you make the height constraint larger? – Bob Sep 30 '18 at 16:14
  • No, everything the same. – Noodledew Sep 30 '18 at 16:17
  • I'm assuming you set the constraints programmatically? If so, check if you set `participateButtonStackView.translatesAutoresizingMaskIntoConstraints = false`. That will let you set your own constraints, otherwise the framework creates some for you and it conflicts with your own. – Bob Sep 30 '18 at 16:29
  • I adopted a custom method for my constraints, which does that automatically for me. – Noodledew Oct 01 '18 at 13:38
  • Can you provide more code? When I run your code and set `translatesAutoresizingMaskIntoConstraints` on the stackview, the error goes away. – Bob Oct 01 '18 at 18:23
  • It's on my github account: https://github.com/Yrleonhelg/LebensfitFirebase You navigate to the problem the following: Click on any day with a red dot, click on a row that appears. Then it's the buttons at the bottom. Search for "FOR_STACKOVERFLOW" to see the .swift file – Noodledew Oct 01 '18 at 18:31
  • Can you tell me what file it's in? – Bob Oct 01 '18 at 18:50
  • LebensfitFirebase -> Tabs -> Events -> SingleEvent -> ParticipateButtonStackView – Noodledew Oct 01 '18 at 18:52
  • ok, is the error being thrown from SingleEventViewController? – Bob Oct 01 '18 at 19:00
  • I don't see `translatesAutoresizingMaskIntoConstraints` called anywhere. – Bob Oct 01 '18 at 19:11
  • It's called in the anchor method to define the constraints. – Noodledew Oct 01 '18 at 19:16
  • 1
    I see it now. I think your best bet is to look at the list of constraints shown above the error message. Try to figure out which ones conflict. I can't imagine any other problem. It's just a matter of taking the time to isolate the conflict. – Bob Oct 01 '18 at 19:45

1 Answers1

0

I was not able to solve this problem so i built this workaround:

ParticipateButton

func setBordersLeftRight() {
    let borderLeft = UIView()
    let borderRight = UIView()
    borderLeft.backgroundColor = .gray
    borderRight.backgroundColor = .gray

    addSubview(borderLeft)
    addSubview(borderRight)
    borderLeft.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0.5, height: 0)
    borderRight.anchor(top: topAnchor, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0.5, height: 0)
}
Noodledew
  • 509
  • 4
  • 19