如何使用Snapkit隐藏其他视图时更新视图的约束?

时间:2017-04-21 06:21:21

标签: ios swift3 snapkit

从上到下有三个视图:redView,yellowView,blueView。 我想隐藏yellowView并更改blueView的约束,以便让blueView在redView下面。 图片如下:   this picture is original   this picture is what I want 代码如下:

private lazy var redView: UIView = {
   let redView = UIView()
    redView.backgroundColor = UIColor.red
    return redView
}()
private lazy var yellowView: UIView = {
   let yellowView = UIView()
    yellowView.backgroundColor = UIColor.yellow
    return yellowView
}()
private lazy var blueView: UIView = {
   let blueView = UIView()
    blueView.backgroundColor = UIColor.blue
    return blueView
}()
override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(redView)
    view.addSubview(yellowView)
    view.addSubview(blueView)
    redView.snp.makeConstraints { (make) in
        make.top.left.right.equalTo(view)
        make.height.equalTo(40)
    }
    yellowView.snp.makeConstraints { (make) in
        make.top.equalTo(redView.snp.bottom)
        make.left.right.height.equalTo(redView)
    }
    blueView.snp.makeConstraints { (make) in
        make.top.equalTo(yellowView.snp.bottom)
        make.left.right.height.equalTo(yellowView)
    }

}

    if yellowView.isHidden == true {
        //how is the code?
    } else {
        //how is the code?
    }

1 个答案:

答案 0 :(得分:0)

您需要为黄色视图设置高度约束,并将IBOutlet作为其黄色视图的高度约束,并根据隐藏/显示修改其常量。

{{1}}