I want to make a UIView rectangular with SnapKit in Swift, like this
lazy var customView: UIView = {
let view = UIView(frame: CGRect())
self.addSubview(view)
view.snp.makeConstraints({ (make) in
make.left.top.bottom.equalToSuperview().inset(self.inset)
make.width.equalTo(make.height) // Error in this line
})
return view
}()
You have to use view.snp.height
instead of make.height
:
lazy var customView: UIView = {
let view = UIView(frame: CGRect())
self.addSubview(view)
view.snp.makeConstraints({ (make) in
make.left.top.bottom.equalToSuperview().inset(self.inset)
make.width.equalTo(view.snp.height) // <---
})
return view
}()
If you have 2 view on same superview, you can do next:
view1.snp.makeConstraints { (make) in
make.leading.equalToSuperview()
make.bottom.equalToSuperview()
make.top.equalToSuperview()
}
view2.snp.makeConstraints { (make) in
make.trailing.equalToSuperview()
make.bottom.equalToSuperview()
make.top.equalToSuperview()
make.leading.equalTo(view1.snp.trailing)
make.width.equalTo(view1.snp.width)
}
and result
In same manner, using view.snp.width
or view.snp.height
you can setup equality of views using SnapKit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With