UIStackView without distribution or filling
Asked Answered
P

2

8

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?)

Phytosociology answered 10/11, 2016 at 17:19 Comment(7)
Have you tried collection view ? Also, have you tried these 4 options ? i.imgur.com/PqbHA65.pngBramblett
@Bramblett I'm looking at collection view now, trying to work out if it will manage the vertical stacking of views for me. I tried all options, they all result in trying to fill the stack view either by making the views larger or spacing them out further, neither is what i want. Thanks for your reply :)Phytosociology
aha, the good thing about collection View is that it is really customizable. Good luckBramblett
@Bramblett Just been looking and i realised that it still requires all the delegate and cell stuff like a tableview does. Given i need a vertical list i may as well use a tableview if i go that route, just trying to see if there's a different approach. Feel like there should be a simpler way to manage a vertical list of views, but maybe i'm procrastinating too muchPhytosociology
I think TableView & Collection are the only way, Apple already created these for you so that you don't have to write something that achieve similar results. Just focus on the design and displaying the data.Bramblett
It will only "make each view bigger vertically to fill the space" if you have constraints telling the stack view which size to be. If you just give constraints to set the stack view's position and width then it will decide it's own height based on the views it contains and do what you want it to do.Colecolectomy
You might want to check this answerLactobacillus
L
1

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

Lag answered 10/11, 2016 at 19:27 Comment(0)
M
0

I managed to do this by setting distribution to fill proportionally and then i pushed at the bottom of the stack a UILabel with some spaces as string. Pushing an empty view or empty UILabel didn't work. I had some constraints errors after this though and didn't investigate further.

Mencher answered 17/5, 2019 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.