Nested UIStackViews Broken Constraints

后端 未结 7 798
故里飘歌
故里飘歌 2021-01-31 02:59

I have a complex view hierarchy, built in Interface Builder, with nested UIStackViews. I get \"unsatisfiable constraints\" notices every time I hide some of my inner stackviews.

7条回答
  •  忘掉有多难
    2021-01-31 03:33

    Here's implementation of Senseful's suggestion #3 written as Swift 3 class using SnapKit constraints. I also tried overriding the properties, but never got it working without warnings, so I'll stick with wrapping UIStackView:

    class NestableStackView: UIView {
        private var actualStackView = UIStackView()
    
        override init(frame: CGRect) {
            super.init(frame: frame);
            addSubview(actualStackView);
            actualStackView.snp.makeConstraints { (make) in
                // Lower edges priority to allow hiding when spacing > 0
                make.edges.equalToSuperview().priority(999);
            }
        }
    
        convenience init() {
            self.init(frame: CGRect.zero);
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        func addArrangedSubview(_ view: UIView) {
            actualStackView.addArrangedSubview(view);
        }
    
        func removeArrangedSubview(_ view: UIView) {
            actualStackView.removeArrangedSubview(view);
        }
    
        var axis: UILayoutConstraintAxis {
            get {
                return actualStackView.axis;
            }
            set {
                actualStackView.axis = newValue;
            }
        }
    
        open var distribution: UIStackViewDistribution {
            get {
                return actualStackView.distribution;
            }
            set {
                actualStackView.distribution = newValue;
            }
        }
    
        var alignment: UIStackViewAlignment {
            get {
                return actualStackView.alignment;
            }
            set {
                actualStackView.alignment = newValue;
            }
        }
    
        var spacing: CGFloat {
            get {
                return actualStackView.spacing;
            }
            set {
                actualStackView.spacing = newValue;
            }
        }
    }
    

提交回复
热议问题