Khoren Markosyan Khoren Markosyan - 1 month ago 20
Swift Question

how to make UIView with equal width and height with SnapKit in Swift?

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

Answer

You have to use view.mas_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.mas_height) // <---
    })
    return view
}()
Comments