Snapkit: How to use aspect ratio constraint?

Created on 18 May 2020  路  1Comment  路  Source: SnapKit/SnapKit

New Issue Checklist

馃毇 If this template is not filled out your issue will be closed with no comment. 馃毇

  • [X] I have looked at the Documentation
  • [X] I have read the F.A.Q.
  • [X] I have filled out this issue template.

Issue Info

Info | Value |
-------------------------|-------------------------------------|
Platform | ios
Platform Version | 13.0
SnapKit Version | latest
Integration Method | cocoapods

Issue Description

I trying to create an aspect ratio constraint 1:1. How can I achieve that?

Most helpful comment

In swift I've done in that way:

extension UIView {
    func aspectRatio(_ ratio: CGFloat) -> NSLayoutConstraint {
        return NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: self, attribute: .width, multiplier: ratio, constant: 0)
    }
}

In snapkit:

view.snp.makeConstraints { (make) in
      make.width.equalTo(view.snp.height).multipliedBy(1.0 / 1.0)
}

>All comments

In swift I've done in that way:

extension UIView {
    func aspectRatio(_ ratio: CGFloat) -> NSLayoutConstraint {
        return NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: self, attribute: .width, multiplier: ratio, constant: 0)
    }
}

In snapkit:

view.snp.makeConstraints { (make) in
      make.width.equalTo(view.snp.height).multipliedBy(1.0 / 1.0)
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Shehryar picture Shehryar  路  7Comments

Cookiezby picture Cookiezby  路  8Comments

cooler333 picture cooler333  路  5Comments

semiwhale picture semiwhale  路  3Comments

romk1n picture romk1n  路  3Comments