IOS stackview addArrangedSubview add at specific index

前端 未结 2 671
长情又很酷
长情又很酷 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)
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 22:20

    You mean you want to insert, not add:

    func insertArrangedSubview(_ view: UIView, atIndex stackIndex: Int)
    
    0 讨论(0)
提交回复
热议问题