Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIStackView without distribution or filling

I have a view on my app where I need to be able to push a dynamic number of custom subviews (the number of views changes whilst its running).

My original idea was a tableview, but it seems a bit OTT setting up all the delegate methods. Then i found UIStackView, which is great as it handles redrawing and resizing when I add and remove the subviews.

However when I have a small number of views in the UIStackView. It will either make each view bigger vertically to fill the space, or distribute them across the stackview depending on the various settings.

What i'd like to have is each view I add just be appended under any existing views in the stack. I don't want distribution or filling of the blank space in the stack. Kind of like a UIStackView with each subview having a height constraint.

I assume this isn't possible with a stackview, so how else can this be achieved? Do I have to use the tableview after all (or fallback to laying it out in code?)

like image 698
TRG Avatar asked Nov 10 '16 17:11

TRG


People also ask

What's a UIStackView did you use it?

UIStackView is useful when you need to repeat same views multiple times like in Sing up view. We use many textfields and manually set constraints between each textfields. But if you put all textfields in stack view then you just need to set required constraints of stackview only and not textfields.

What is fill proportionally in UIStackView?

case fillProportionally. A layout where the stack view resizes its arranged views so that they fill the available space along the stack view's axis. Views are resized proportionally based on their intrinsic content size along the stack view's axis.

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.

How can I change background color in Stackview?

You can't do this – UIStackView is a non-drawing view, meaning that drawRect() is never called and its background color is ignored. If you desperately want a background color, consider placing the stack view inside another UIView and giving that view a background color. Reference from HERE.


1 Answers

If your views have a defined height constraint, this should work with the Distribution being Equal Spacing.

like image 71
GetSwifty Avatar answered Oct 06 '22 23:10

GetSwifty