使用snapkit使用两个uiviews创建约束

时间:2016-10-29 18:53:51

标签: ios swift uiview

我想在底部装一个UIView,静态高度为60,然后是顶部的UIView填充其余部分。但是这段代码似乎只是让最顶层的代码填满整个屏幕。

    //bottomWrapperView
    let bottomWrapperView = UIView()
    bottomWrapperView.backgroundColor = UIColor.red
    self.addSubview(bottomWrapperView)

    //TopWrapperView
    let topWrapperView = UIView()
    topWrapperView.backgroundColor = UIColor.green
    self.addSubview(topWrapperView)


    //BottomWrapperView Constraints
    bottomWrapperView.snp.makeConstraints { (make) -> Void in
        make.height.equalTo(60)
        make.left.equalTo(self).offset(0)
        make.bottom.equalTo(self).offset(0)
        make.right.equalTo(self).offset(0)
        make.top.equalTo(topWrapperView)
    }



     //TopWrapperView Constraints
    topWrapperView.snp.makeConstraints { (make) -> Void in

        make.left.equalTo(self).offset(0)
        make.top.equalTo(self).offset(0)
        make.bottom.equalTo(bottomWrapperView)
        make.right.equalTo(self).offset(0)
    }

1 个答案:

答案 0 :(得分:0)

在这里,您需要将topWrapperView的底部约束设为等于bottomWrapperView的顶部

//BottomWrapperView Constraints
bottomWrapperView.snp.makeConstraints { (make) -> Void in
    make.height.equalTo(60)
    make.left.equalTo(self).offset(0)
    make.bottom.equalTo(self).offset(0)
    make.right.equalTo(self).offset(0)
}

//TopWrapperView Constraints
topWrapperView.snp.makeConstraints { (make) -> Void in
    make.left.equalTo(self).offset(0)
    make.top.equalTo(self).offset(0)
    make.bottom.equalTo(bottomWrapperView.snp.top)
    make.right.equalTo(self).offset(0)
}