I would like to setup a UIViewStack so that it will center the two views inside, even though they have different widths. Here is an example:
Is it possible to achieve this type of configuration with UIStackView? I cannot seem to figure it out!
Any help would be appreciated.
The proper way to set the UIScrollView including UIStackView is to use option #1. Set the width of the UIStackView equal to the UIViewController view. Also add the height constraint to UIStackView marked as removed at build time.
We have covered some usages of UIStackView but it has much more to offer. You can work on content hugging and content compression resistance priorities for changing the views’ distributions, you can add variations for different size classes, etc. Of course, stack view can make you angry from time to time.
This creates a horizontal stack view for the row. Next, position these rows horizontally, select them, and click the Editor > Embed In > Stack View menu item again. This creates a horizontal stack of rows.
The outer stack view is stretching the inner stack view to fill the width, but the inner stack view allows the label to keep its original width! Build and run. Uh-oh. Why is the Hide button hanging out in the middle of the text? When you embedded the WEATHER label in a stack view, any constraints between it and the Hide button were removed.
You should use nested StackView
. Firstly embed View1 and View2 in a Horizontal StackView
. Set alignment property center and distribution fill-proportionally. Then embed the Horizontal StackView
in a Vertical Stackview
. Here I have attached my demo screenshot:
No , you can't . From the apple's Doc
The stack view uses Auto Layout to position and size its arranged views. The stack view aligns the first and last arranged view with its edges along the stack’s axis.
In a horizontal stack, this means the first arranged view’s leading edge is pinned to the stack’s leading edge, and the last arranged view’s trailing edge is pinned to the stack’s trailing edge.
You can use Constraints instead.
I can make UIStackView auto grow width after adding views there
Codes
class StackSampleViewController: UIViewController {
@IBOutlet weak var stackView: UIStackView!
//Keep center auto grow with subviews
@IBAction func touchUpAdd(_ sender: Any) {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
if (stackView.subviews.count % 2) == 0 {
view.widthAnchor.constraint(equalToConstant: 100).isActive = true
view.backgroundColor = .black
} else {
view.widthAnchor.constraint(equalToConstant: 50).isActive = true
view.backgroundColor = .red
}
stackView.addArrangedSubview(view)
}
}
Result
Yes you can, firstly you create main vertical stackview and set aligment center and distribution fill. Then you create second horizontal stackview in main stack and set alignment and distribition fill. Add the last elements you want to add Thats it.
First stackview:
Second stackview:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With