Programmatically add subviews to a UIStackView

社会主义新天地 提交于 2019-12-04 03:48:14

As Dan said in the comments, use addArrangedSubview to add a subview that will be automatically laid out by UIStackView.

Note, however: if you remove a view from the arrangedSubviews array on UIStackView, it is still a subview. From the UIStackView documentation:

The stack view ensures that its arrangedSubviews property is always a subset of its subviews property. Specifically, the stack view enforces the following rules:

• When the stack view adds a view to its arrangedSubviews array, it also add that view as a subview, if it isn’t already.

• When a subview is removed from the stack view, the stack view also removes it from the arrangedSubviews array.

• Removing a view from the arrangedSubviews array does not remove it as a subview. The stack view no longer manages the view’s size and position, but the view is still part of the view hierarchy, and is rendered on screen if it is visible.

It's always a good idea to start with the documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!