Search code examples
iosuibuttonuipickerviewuistackviewxcode-storyboard

Replace view inside UIStackView


I have a UIStackView with 5 buttons inside. One of those buttons (#4) needs to be replaced by a UIPickerView once it is clicked and switched back to the UIButton once one of the other four's is selected.

The UIStackView is horizontal, alignment is fill and distribution is Fill Proportionally.

The stack was done using storyboard, and I am trying to replace the view with:

    override func viewDidLayoutSubviews(){
        self.pickerView.frame.origin = self.view.viewWithTag(40)!.frame.origin
        self.pickerView.frame.size = CGSize(width: 30, height: 30)

}

But my UIStackView will not rearrange after the button click, and button #5 is staying on top of the UIPickerView.

Any ideas?


Solution

  • Ok, I found the code. I had to replace addView for addArrangedSubview.

    Final code (on button press):

        let stack = (self.view.viewWithTag(20) as! UIStackView)
        stack.addArrangedSubview(self.pickerView)