IOS stackview addArrangedSubview add at specific index IOS stackview addArrangedSubview add at specific index ios ios

IOS stackview addArrangedSubview add at specific index


You mean you want to insert, not add:

func insertArrangedSubview(_ view: UIView, atIndex stackIndex: Int)


if you don't want to struggle with the index you can use this extension

extension UIStackView {    func insertArrangedSubview(_ view: UIView, belowArrangedSubview subview: UIView) {        arrangedSubviews.enumerated().forEach {            if $0.1 == subview {                insertArrangedSubview(view, at: $0.0 + 1)            }        }    }        func insertArrangedSubview(_ view: UIView, aboveArrangedSubview subview: UIView) {        arrangedSubviews.enumerated().forEach {            if $0.1 == subview {                insertArrangedSubview(view, at: $0.0)            }        }    }}