Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center views horizontally of different widths inside a UIStackView

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:

example

Is it possible to achieve this type of configuration with UIStackView? I cannot seem to figure it out!

Any help would be appreciated.

like image 768
Jason Silberman Avatar asked Nov 08 '17 00:11

Jason Silberman


People also ask

How to set the height of the UIScrollView including the uistackview?

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.

What is uistackview and how to use it?

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.

How to create a horizontal stack view for a row?

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.

What is the difference between the outer and inner stack view?

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.


4 Answers

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: enter image description here

like image 100
Muzahid Avatar answered Nov 15 '22 10:11

Muzahid


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.

like image 23
dengApro Avatar answered Nov 15 '22 10:11

dengApro


I can make UIStackView auto grow width after adding views there

  1. Create UIViewController and give UIStackView be center there enter image description here
  2. Constraint StackView.leading Priority set to 250 to avoid warning issue in xib enter image description here
  3. 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)
    }
    

    }

  4. Result enter image description here

like image 37
Hsiao-Ting Avatar answered Nov 15 '22 11:11

Hsiao-Ting


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: First stackview

Second stackview: second stackview

like image 28
Umut ADALI Avatar answered Nov 15 '22 10:11

Umut ADALI