I am trying to build a stack view with UIStackView
that always shows 3 views at max, but can also show none, 1 or 2 views at the same time.
The whole stack view is supposed to be dynamic. At the beginning the stack view is empty. Another function will add a new view at the top with stackView.insertArrangedSubview(at: 0)
. If another view is added, then the previous view at index 0 should be pushed to index 1. That's why I picked the UIStackView
, since it takes care of the reordering of existent views.
When a third view is pushed in, the other two will move down a slot again. Now, in case a fourth view get pushed in, all previous three views will move down again, but the fourth will then be removed from the stack view.
Here comes a small extra condition though:
The view at the first index should always be double the size of the remaining 2 views. So first view should be 50% of the whole stack view. Views at index 1 and 2 should therefore be 25% in height.
If a new view is pushed in, the currently at index 0 view will therefore have to shrink, move down to the middle, and be moved to index 1 of the stack view.
The idea is similar to how Growl Notification System work.
This image shows the basic layout of how the views should look.
SO the question now is, are UIStackView
s the correct way to tackle this issue or would be working with constraints only (e.g. with SnapKit
) be a better way? Even with UIStackView
I would probably need to have a lot of constraints to fulfil the requirements.
So maybe I should just have an addNewView
function that updates all constraints and call layoutIfNeeded
in a UIView.animate
block to have the transition of a view pushing down the others and the last view getting pushed out?
What do you guys think? If there exists a framework that already does something similar, I would appreciate pointers as well.