IOS stackview addArrangedSubview add at specific index

前端 未结 2 670
长情又很酷
长情又很酷 2021-02-06 21:42

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

something like:

stackView.addArrangedSubview(nibView, atIndex: inde         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 22:11

    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)
                }
            }
        }
    }
    

提交回复
热议问题