As with iOS 9, you can use UIStackView
, which is very similar to LinearLayout
: you add views, and the stack view arranges them as needed based on your size parameter - if you use Interface Builder, you can experiment with each option so that see which one suits your needs. You can also set the distance between the views in the stack view by adding some extras.
WARNING When adding views of stack children in your code, you should always use addArrangedSubview()
as follows:
stackView.addArrangedSubview(someView)
If you try to use the plain old addSubview()
, it will not work correctly because the stack view will not know to organize it.
As for deletion, you need to be careful to use stackView.removeArrangedSubview(someView)
and someView.removeFromSuperview()
, otherwise the view will not be deleted correctly.
You can find the helpful UIStackView tutorial .
source share