NojDavid
NojDavid

Reputation: 89

Add view to stack view and change height constraint to maintain ratio

When adding a new view to a stack view, I want to maintain the ratio of views inside the the stackview by increasing the stackview height constraint by the amount of the added view. I've been reading up on how to update view constraints but cant seem to make it work.

self.stackView?.insertArrangedSubview(self.userErrorLabel, at: 2)
self.heightConstraint = self.stackView?.heightAnchor.constraint(equalToConstant: 140 + self.stackView?.bounds.height)
self.stackView?.addConstraints([self.heightConstraint!])
self.stackView?.setNeedsLayout()

Any advice would be extremely helpful!!

Upvotes: 0

Views: 1243

Answers (1)

Ghean
Ghean

Reputation: 21

Are you using XIB or story board? you can register the height constraint as Outlet to your View/VC and then you can edit / increase the height there. let say that you register the height constraint as

@IBOutlet weak var stackViewHeight: NSLayoutConstraint!

all you need to do is just

let currentStackHeight = stackViewHeight.constant
stackViewHeight.constant = currentStackHeight + self.addedView.bounds.height

and just like that, you don't need to add or remove any constraint

Upvotes: 0

Related Questions