how to make UIView with equal width and height with SnapKit in Swift?
12,448
Solution 1
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
}()
Solution 2
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
Related videos on Youtube
Author by
Khoren Markosyan
Updated on June 04, 2022Comments
-
Khoren Markosyan almost 2 years
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 }()
-
Peyman Mohamadpour about 6 yearsPlease add some explanations