-1

i'm trying to add a button back into a stackview and i'm not sure how to do it in the code, i only want the button to appear under a circumstance so that's why i'm doing it in code, here's what i have

i'm removing it with

func hideCompareButton() {
    UIView.animateWithDuration(0.4, animations: {
        self.compareButton.alpha = 0
        }) { completed in
            if completed == true {
                self.compareButton.removeFromSuperview()
            }
    }
}

and i've got this that would normally work with other views, but i'm not sure how to add a button onto a stackview, at a prefered locations, there's 3 buttons when it's not added, 4 when it is, and id like it to be the 3rd button in from the left

func showCompareButton() {
    // bottomStackView.addArrangedSubview(compareButton) ???
    bottomStackView.addSubview(compareButton)

    let bottomConstraint = compareButton.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor)
    let leftConstraint = compareButton.leftAnchor.constraintEqualToAnchor(filterButton.rightAnchor)
    let rightConstraint = compareButton.rightAnchor.constraintEqualToAnchor(shareButton.leftAnchor)

    let heightConstraint = compareButton.heightAnchor.constraintEqualToConstant(44)

    NSLayoutConstraint.activateConstraints([bottomConstraint, leftConstraint, rightConstraint, heightConstraint])

    bottomStackView.layoutIfNeeded()
}
AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
Allen Huskins
  • 65
  • 2
  • 12

2 Answers2

2

Why don't you build the stack view the way you want it with all the buttons and then simply hide the 3rd button. When you want it unhide it. The stack view will do all the rest. Here some stack view code to get you going. This builds a keypad, but its the same principle, it adds all the buttons and then makes it visible.

Here some stack view code to get you going.

    keyPadWindow = UIView(frame: CGRect(x: 128, y: 224, width: 512, height: 356))
    var keyCount = 0
    keyPadSV = UIStackView(frame: CGRect(x: 0, y: 0, width: 512, height: 356))
    print("pickerWindow.bounds \(pickerWindow.bounds)")
    keyPadSV!.axis = .Vertical
    keyPadSV!.spacing = 0.0
    keyPadSV!.alignment = .Center
    keyPadSV!.distribution = .EqualSpacing
    for var k2s = 0; k2s < 5; k2s++ {
        let keyPadSVB = UIStackView(frame:CGRect(x: 0, y: 0, width: 0, height: 0))
        keyPadSVB.axis = .Horizontal
        keyPadSVB.spacing = 0.0
        keyPadSVB.alignment = .Center
        keyPadSVB.distribution = .EqualSpacing
        for var k4s = 0; k4s < 4; k4s++ {
            let button   = UIButton(type: UIButtonType.Custom) as UIButton
            button.frame = CGRectMake(0, 0, 0, 0)
            button.tag =  keyCount
            let blah = "x" + String(keyCount)
            let blahIMAGE = UIImage(named: blah)
            button.setImage(blahIMAGE, forState: UIControlState.Normal)

            print("blah \(blah)")
            button.addTarget(self, action: "keyPadPress:", forControlEvents: UIControlEvents.TouchUpInside)
            keyPadSVB.addArrangedSubview(button)
            keyCount++
        }
        keyPadSV.addArrangedSubview(keyPadSVB)
    }
    keyPadWindow.addSubview(keyPadSV)
    self.view.addSubview(keyPadWindow)
}
user3069232
  • 8,587
  • 7
  • 46
  • 87
1

Don't add any constraints! That is exactly the job of the stack view. Just make the button an arranged view of the stack view and stop.

If the problem is that the button is to be inserted in the midst of the existing arranged views, well, that is what insertArrangedSubview:atIndex: is for. See IOS stackview addArrangedSubview add at specific index

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • why don't you build the stack view the way you want it with all the buttons and then simply hide the 3rd button. When you want it unhide it. The stack view will do all the rest. – user3069232 Mar 10 '16 at 19:33
  • Hey @user3069232 that's such a good answer that you should give it as an answer. I'll upvote it! – matt Mar 10 '16 at 19:35
  • Thanks Matt, good to have some encouragement for a change! This is a great site although I fear it maybe a decade or more before I get to you level :) – user3069232 Mar 10 '16 at 20:01
  • insertArrangedSubview:atIndex works great, althought it's putting in a blank button that i can't click with no text, but ill figure out how to add the constraints, i do have @IBOutlet var shareButton: UIButton! connected but it just does a blank – Allen Huskins Mar 10 '16 at 20:34
  • Well, your code does not show where `compareButton` comes from or where it is is being maintained. But I assure you that if you have a proper button, this will work. – matt Mar 10 '16 at 20:45