0

I've tried to add buttons dynamically/programmatically in UIStackView that I've built with interface builder but they failed to show up when I run the application. The number of buttons that's supposed to be added ranging normally from 4-6. Can you guys tell me what's wrong with the code

5 buttons 4 buttons code

3
  • Probably some sizing issues. Commented Sep 30, 2019 at 6:31
  • I've already set the size & constraint of the stackView using the Interface Builder. @MojtabaHosseini Commented Sep 30, 2019 at 6:33
  • 1
    Do not add Solved to title. Commented Sep 30, 2019 at 6:59

3 Answers 3

17

@Nowonder I just recreate what you are trying to achieve. The following are the steps.

  1. Add a UIStackView in viewController from Interface Builder and add required constraints.
  2. Add the following code.

    override func viewDidLoad() {
       super.viewDidLoad()
    
       let button = UIButton()
       button.setTitle("btn 1", for: .normal)
       button.backgroundColor = UIColor.red
       button.translatesAutoresizingMaskIntoConstraints = false
    
       let button2 = UIButton()
       button2.setTitle("btn 2", for: .normal)
       button2.backgroundColor = UIColor.gray
       button2.translatesAutoresizingMaskIntoConstraints = false
    
       let button3 = UIButton()
       button3.setTitle("btn 3", for: .normal)
       button3.backgroundColor = UIColor.brown
       button3.translatesAutoresizingMaskIntoConstraints = false
    
       buttonStackView.alignment = .fill
       buttonStackView.distribution = .fillEqually
       buttonStackView.spacing = 8.0
    
       buttonStackView.addArrangedSubview(button)
       buttonStackView.addArrangedSubview(button2)
       buttonStackView.addArrangedSubview(button3)  
    
    }
    

Following is the outcome. enter image description here

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to do few more steps before the button will start showing up.

  1. Add the stack view to current view's subviews

    view.addSubview(buttonStackView)
    
  2. Now each of these views buttons, as well as the stackView, needs to set the translatesAutoresizingMaskIntoConstraints to false.

    button1.translatesAutoresizingMaskIntoConstraints = false
    button2.translatesAutoresizingMaskIntoConstraints = false
    buttonStackView.translatesAutoresizingMaskIntoConstraints = false
    
  3. Now set the stackView contraints

    NSLayoutConstraint.activate([
        buttonStackView.topAnchor.constraint(equalTo: view.topAnchor),
        buttonStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        buttonStackView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        buttonStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor)])
    
  4. You will need to provide the constraints, or can override the intrinsic size of stackView.

     override func intrinsicContentSize() -> CGSize
     {
       return CGSizeMake(200, 40)
     }
    

As UILabel have the intrinsic size and so, the button will show, if not you will have to set the constraints for them also to be safe.

1 Comment

The buttons eventually show up when I specify the type of the buttons. However, I'll try your method later
-1

SOLVED!

The button showed up when I set the type of the button with the instance method init(type:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.