为 UIStackView 设置排序顺序

Set sort order for UIStackView

是否可以为 UIStackView 设置排序顺序?例如,我想按时间排序。每个子视图都有一个 time 属性 这只是一个 Int.

我知道您可以通过对数组进行排序然后刷新 table 来对 UITableView 进行排序,但是 UIStackView 没有这样的委托和数据源,所以我不知道知道该怎么做。它似乎只是按照您添加子视图的顺序进行,这使得在不先删除所有内容的情况下在中间添加视图具有挑战性。

也许读 the documentation?

The stack view manages the layout of all the views in its arrangedSubviews property. These views are arranged along the stack view’s axis, based on their order in the arrangedSubviews array.

您可以删除所有已排列的子视图,然后按照您自己的顺序重新添加或插入它们。确保这个子视图没有被隐藏,因为 stackView 将它们排序到 arrangedSubviews 数组的头部