Peter Pik Peter Pik - 1 month ago 19
iOS Question

Creating constraints with two uiviews using snapkit

i'm tying to crate one UIView at the bottom with static height 60 and then a top one filling the rest. however this code seem to just make the top one fill the whole screen.

//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)
}

Answer

Here, you need to make bottom constraint for topWrapperView equal to the top of 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)
}