Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left aligned horizontal stackview and top aligned vertical stackview

I have two StackView, one of which is horizontal and another vertical.

For the horizontal StackView, I want the items to be aligned to the left and grow toward the right.

For the vertical StackView, I want the items to be aligned to the top and grow downward. For example the position of the top item would never change but the stackview expands down if another item is added.

I'm having trouble accomplishing this, for example with my horizontal StackView, if I have two items 40x40, they will example the entire width of screen for some reason and I've tried a whole slew of different variations of distribution, alignment and constraints.

For my vertical StackView, if I add two items, my first item will move up and the second item will go underneath, so the center is now between the two instead of the first item staying still and the second going underneath it.

I can't figure out what I'm doing wrong here and how to just get StackViews where the first item is sized correctly, in place, and future items follow it, expanding either rightwards or downwards.

like image 895
Brejuro Avatar asked Jun 29 '18 15:06

Brejuro


People also ask

What is stackView alignment?

A layout where the stack view aligns the center of its arranged views with its center along its axis. case leading. A layout for vertical stacks where the stack view aligns the leading edge of its arranged views along its leading edge.

What is stackView in Swift?

Overview. Stack views let you leverage the power of Auto Layout, creating user interfaces that can dynamically adapt to the device's orientation, screen size, and any changes in the available space. The stack view manages the layout of all the views in its arrangedSubviews property.

How to add stack view?

Top-level Stack ViewCommand-click to select all five top-level stack views in the outline view. Then embed them all in one Stack View: Click the Add New Constraints button, check Constrain to margins and add constraints of 0 to all edges. Then set Spacing to 20 and Alignment to Fill.


1 Answers

What you need is for Vertical

1- Drag a UIStackView with top , leading and trailing constraints to the superView

2- Drag any view inside it and give it a height constraint

for Horizontal

1- Drag a UIStackView with top , leading and height constraints to the superView

2- Drag any view inside it and give it a width constraint

Note : Make distribution set to fill

like image 167
Sh_Khan Avatar answered Sep 30 '22 17:09

Sh_Khan