Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS stackview addArrangedSubview add at specific index

How is it possible to add a arranged subview in a particular index in a UIStackView?

something like:

stackView.addArrangedSubview(nibView, atIndex: index)
like image 417
Elton Garcia de Santana Avatar asked Dec 23 '15 11:12

Elton Garcia de Santana


2 Answers

You mean you want to insert, not add:

func insertArrangedSubview(_ view: UIView, atIndex stackIndex: Int)
like image 163
Wain Avatar answered Nov 20 '22 11:11

Wain


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)
            }
        }
    }
}
like image 1
chrigu Avatar answered Nov 20 '22 10:11

chrigu