I am trying to figure out how to make my three buttons in a stackview have relative sizing and keep the same multiplier spacing on different devices, e.g. the buttons will be bigger if i am on an ipad compared to an iphone. Secondly the spacing between left and right edges of buttons will be bigger on an ipad compared to that of an iphone device. So far I currently have three buttons in a stackview. I have added horizontally and vertical allignment to my stackview. I have played around with adding equal heights and width and changed the multiplier as well as adding constraints to the buttons however it did not get my desired result. Here is a screenshot of how i'd like my items to be placed on all devices:
UIStackView
has only static spacing. You have two options:
viewWillLayoutSubviews
. I prefer it over viewDidLayoutSubviews
because changes will follow rotation animation. But if your calculations will depend on other subviews(not just self.view
), these frames will not be updated yet. You can change constraint.constant
like this too.@IBOutlet var stackView: UIStackView!
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
stackView.spacing = view.bounds.height * 0.05
}
UIStackView
and for plain views.I prefer adding equal height constraint for all views with same size(like btn1.height = btn2.height, btn1.height = btn3.height
), so I can set size with one constraint to all of them(btn1.height = superview.height * 0.1
).
Something like this should work for your:
Result: