letsCode
letsCode

Reputation: 3044

UIStackView height, content inside in the middle not at top

I have a UIStackview, and when its filled with a bunch of things, its fine, However, when its filled with 1, or 2 (for example), it spreads them out and not keeps them at top. What did I do wrong?

enter image description here

This is what happens. I want them to be at the top and with No right under it. enter image description here

for i in 0..<self.itemSpecificsArray.count {
                    let itemSpecificName = UILabel()
                    let itemSpecificValue = UILabel()

                    itemSpecificName.textColor = UIColor(red: 236.0 / 255.0, green: 91.0 / 255.0, blue: 110.0 / 255.0, alpha: 1.0)
                    itemSpecificName.textAlignment = .left
                    itemSpecificName.font = UIFont.boldSystemFont(ofSize: 18.0)

                    itemSpecificValue.textColor = UIColor.black
                    itemSpecificValue.textAlignment = .left
                    itemSpecificValue.font = UIFont.systemFont(ofSize: 15.0)

                    print(self.itemSpecificsArray[i].itemSpecificName)
                    print("** \(self.itemSpecificsArray[i].itemSpecificValue)")
                    itemSpecificName.text = self.itemSpecificsArray[i].itemSpecificName
                    itemSpecificValue.text = self.itemSpecificsArray[i].itemSpecificValue

                    self.mainSectionItemSpecifics.addArrangedSubview(itemSpecificName)
                    self.mainSectionItemSpecifics.addArrangedSubview(itemSpecificValue)
                     if (i == self.itemSpecificsArray.count) {
                        let view = UIView()
                        self.mainSectionItemSpecifics.addArrangedSubview(view)
                    }
                }

For each index, it has a itemSpecificName and itemSpecificValue. If I need to put these two Labels inside of a View, then I can do that...That will probably fix it, wouldnt it?

Upvotes: 0

Views: 48

Answers (1)

rob mayoff
rob mayoff

Reputation: 385998

UIStackView wants to stretch its arranged subviews to fill its own (the stack view's) bounds. If you don't want the labels stretched, you either need to change the constraints on the UIStackView so that it can shrink to fit its children, or add an arranged subview to the stack view that can absorb the extract space. For example, you can add a plain UIView with no constraints after the two labels. Auto layout will give the extra space of the stack view's bounds to the UIView.

Upvotes: 1

Related Questions