Friday, 1 November 2019

Dynamic number of subviews to take up maximum space in superview

I have a superview with an arbitrary amount of subviews. The app I'm creating has the ability to continually add subviews into this superview. I want the subviews to fill as much space as possible like shown in the pictures below. These subviews will have an aspect ratio of 5:8

enter image description here enter image description here

My idea was to add one horizontal stack view inside of a vertical stackview initially. The number of inner stackviews should be equal to the max number of cards per row, so if √(numberOfViews) > numberOfStackViews I could add another inner stack view. I could loop through each inner stack view adding subviews one at a time in order to assure they all get the same number of views.

I'm wondering if there is an easier solution. Also this solution does not work for the case of sideways orientation.



from Dynamic number of subviews to take up maximum space in superview

No comments:

Post a Comment