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

Khoren Markosyan picture Khoren Markosyan · Oct 26, 2016 · Viewed 10.7k times · Source

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

d.felber picture d.felber · Oct 26, 2016

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