Fixed height constraints for UIStackview subqueries

I have UIScrollView

one that has a subtext UIStackView

, name it stack

. I want to have a dynamic number of subheadings in stack

, but these subheads will have different heights. How can I achieve this? Please note that I am trying to dodge the storyboard.

let scrollView = UIScrollView(frame: CGRect(x: 20, y: 20, width: view.frame.width - 20, height: view.frame.height - 20))
scrollView.center = view.center
self.view.addSubview(scrollView)


let stack = UIStackView(frame: CGRect(x: 20, y: 20, width: scrollView.frame.width - 40, height: scrollView.frame.height - 40))
stack.axis = .vertical
stack.alignment = .fill
stack.distribution = .fillProportionally
stack.spacing = 20
scrollView.addSubview(stack)

let label1 = UILabel()
label1.text = "This is going to be very, very long. I don't know how to make this guy longer. Words and words and more words. // Do any additional setup after loading the view, typically from a nib."
label1.numberOfLines = 0
label1.sizeToFit()

// Add more labels similar to label1

      

I tried adding a constraint to each of my subframes UILabel

, but that doesn't really work.

let x = NSLayoutConstraint(item: label1, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: label1.frame.size.height)
label1.addConstraint(x)

      

Please, help! Thank.

+3


source to share





All Articles