Swift - Adding labels to stack views programmatically

squarehippo10

My goal is to add labels to a stackview in a xib. Everything below works great if the results are being displayed using a view controller. But I don't need to display the view I'm creating. I just want to add it to an array. (The array will be converted to a PDF later.) For some reason, if I just save the view, anything in the stack view will not appear when the PDF is created. My actual xib has many other fields that show up just fine. It's only the fields I try to programmatically add as labels to a stack view that fail to appear!

This is my xib. The stack view is pinned to the edges and it's set to fill equally. enter image description here

And this is the code for the xib. There's one outlet for the view and one for the stack view.

import UIKit

class ContainerView: UIView {

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var testStackView: UIStackView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    func setup() {
        Bundle.main.loadNibNamed("ContainerView", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
    }
}

And finally, here is the class that adds labels to the stack view. It has been simplified considerably.

import UIKit

class PreparePDF : UIView {

    func addMyArrangedSubviews (
        let containerView = ContainerView()

        let testLabel1 = UILabel()
        testLabel1.text = "Hola"
        testLabel1.backgroundColor = .red

        let testLabel2 = UILabel()
        testLabel2.text = "Hello"
        testLabel2.backgroundColor = .yellow

        containerView.testStackView.addArrangedSubview(testLabel1)
        containerView.testStackView.addArrangedSubview(testLabel2)  

        pdfPages.append(containerView)
}

I've tried to ask this question several times, but I think I was making it sound too complicated. I'm not sure this is any better!

Josh Homann

You are creating a view with: let containerView = ContainerView(), then you are adding labels to its stackView, then you hit the end of the function and containerView is deinited because you did not retain it or add it as a subview.

Your init for ContainerView is also wrong. if you call init it will not load the nib file. You should call UINib.instantiate(withOwner:options:) instead and move your setup code to awakeFromNib and fatalError the constructors so no-one tries to make the nib programmatically and bypass the nib file (in which case the outlets would not be connected).

let containerView = UINib(nibName: String(describing: ContainerView.self), bundle: nil)
      .instantiate(withOwner: nil, options: nil).first as! ContainerView

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift - StackView and Adding Constraints With Programmatically Added Views To Stack - Adjust Cell Hight After Load?

Programmatically adding views to a UIStackView

Adding Layout with views programmatically

Swift - create views programmatically

Switching Views Programmatically in Swift

Programmatically switching views swift

Swift Nested Stack Views

Adding labels programmatically, aligned with labels from Storyboard

Positioning miltiple views programmatically in swift

Adding views programmatically. displays nothing

Adding views programmatically to LinearLayout but they don't appear

Adding views programmatically cause change of size in Android

Adding Views. Storyboard VS. Programmatically

Adding buttons to toolbar programmatically in swift

Swift Spritekit Adding Button programmatically

Adding UIGestureRecognizer To Subview Programmatically in Swift

iOS adding constraints programmatically in Swift

How to add reusable custom views programmatically? (Swift)

iOS Swift: Install and Unistall views programmatically

Swift - How put a stack of views into a scrollview

How can I stack views programmatically without manually calculating offsets?

Nested Stack Views: Child Stack View is not inside its parent stack view when attaching it programmatically

Error when programatically adding constraints to views in Swift

Adding navigation bar on all the views. Swift

Adding views to LinearLayout programmatically not setting text on all but one

Scrolling with ScrollView not working after adding views programmatically to hierarchy

Relative Layout adding views programmatically one below other not aligning correctly

Views not displaying correctly after adding them to NSStackView programmatically

Adding the sum of labels from a table cell in Swift