UIStackView : Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?

后端 未结 8 796
遥遥无期
遥遥无期 2021-02-02 05:04

From the UIStackView Class Reference

In removeArrangedSubview:

To prevent the view from appearing on screen after calling the stack’s removeArrang

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 05:39

    For removing i use this extension. Don't really know is it necessary to remove constraints. But maybe it would help.

    extension UIStackView {
        func removeAllArrangedSubviews() {
            let removedSubviews = arrangedSubviews.reduce([]) { (allSubviews, subview) -> [UIView] in
                self.removeArrangedSubview(subview)
                return allSubviews + [subview]
            }
    
            for v in removedSubviews {
                if v.superview != nil {
                    NSLayoutConstraint.deactivate(v.constraints)
                    v.removeFromSuperview()
                }
            }
        }
    }
    

提交回复
热议问题