1

I am trying to build a vertical stack view with a UI label and multiple horizontal stack views, However, I only see the multiple horizontal stacks in the UI and not the UI label. Here is some code

final class FinalView: UIView {

   let mainStackView = UIStackView()

     init() {
        super.init(frame: .zero)

         let label: UILabel = UILabel()
         label.translatesAutoresizingMaskIntoConstraints = false
         label.text = "Sorry ?"

               ...

        let verticalStackView = UIStackView(arrangedSubviews: multiplehorizontalViews)
        verticalStackView.axis = .vertical
        verticalStackView.translatesAutoresizingMaskIntoConstraints = false
        verticalStackView.spacing = 5.0

         mainStackView.addArrangedSubview(label)
         mainStackView.addArrangedSubview(verticalStackView)
         mainStackView.translatesAutoresizingMaskIntoConstraints = false
         mainStackView.axis = .vertical
   }
}

I am only seeing verticalStackView but not label. When I removed this mainStackView.addArrangedSubview(verticalStackView) from the code, I can see the label. But When it is there I can't see the label. Any ideas on the issue or any tips for debugging?

Nari
  • 333
  • 5
  • 13

2 Answers2

0

Your code is working completely fine. Just checked on iOS 13 with below code

let mainStackView = UIStackView()
    mainStackView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(mainStackView)

    var vflString = "V:|-[mainStackView]-|"
    var vflConstraints = NSLayoutConstraint.constraints(withVisualFormat: vflString, options: [], metrics: nil, views: ["mainStackView" : mainStackView])
    view.addConstraints(vflConstraints)

    vflString = "H:|-[mainStackView]-|"
    vflConstraints = NSLayoutConstraint.constraints(withVisualFormat: vflString, options: [], metrics: nil, views: ["mainStackView" : mainStackView])
    view.addConstraints(vflConstraints)


    let label: UILabel = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "Sorry ?"

    let label1 = UILabel(frame: .zero)
    label1.text = "Label 1"
    let label2 = UILabel(frame: .zero)
    label2.text = "Label 2"
    let label3 = UILabel(frame: .zero)
    label3.text = "Label 3"

    let multiplehorizontalViews = [label1, label2, label3]

    let verticalStackView = UIStackView(arrangedSubviews: multiplehorizontalViews)
    verticalStackView.axis = .horizontal
    verticalStackView.translatesAutoresizingMaskIntoConstraints = false
    verticalStackView.spacing = 5.0
    verticalStackView.distribution = .fillProportionally

    mainStackView.addArrangedSubview(label)
    mainStackView.addArrangedSubview(verticalStackView)
    mainStackView.translatesAutoresizingMaskIntoConstraints = false
    mainStackView.axis = .vertical
Manoj
  • 1,019
  • 9
  • 17
  • Not sure why it isn't working for me.Is there a way to add `label ` directly `let verticalStackView = UIStackView(arrangedSubviews: multiplehorizontalViews)` – Nari Apr 11 '20 at 13:04
  • Can you share a screenshot, so I can better see what exactly is failing? Also is there any constraints that are breaking in your view? – Manoj Apr 11 '20 at 13:16
0

I'm pretty sure it's not your case but in mine this applied:

UIBezierPath: roundedRect: byRoundingCorners: cornerRadii: acts weird

bezier path on layer was cutting all content from the stackview

you can check in the visual debugger what's going in: if the labels text show up there then it's an issue with layer clipping

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66