Snapkit: how can I see the constants of the constraints I make on a view?

Created on 14 Jul 2016  路  2Comments  路  Source: SnapKit/SnapKit

New Issue Checklist

| Info | Value |
| --- | --- |
| Platform | e.g. ios/osx/tvos |
| Platform Version | e.g. 8.0 |
| SnapKit Version | e.g. 0.19.0 |
| Integration Method | e.g. carthage/cocoapods/manually |

Issue Description

such as

i make constraints on redView like this:

redView.snp_makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(100)
            make.top.equalTo(view).offset(20)
            make.left.equalTo(view).offset(20)
        }

how can i get the constant of the constraints on readView?
var viewHeight: NSLayoutConstraint!
viewHeight.constant

Most helpful comment

You can always keep a reference to constraints from constraint:

var redViewWidthConstraint: Constraint?
redView.snp_makeConstraints { (make) in
           redViewWidthConstraint = make.width.equalTo(100).constraint
        }

Since a Constraint object has layoutConstraints: [LayoutConstraint], you can get any of its constraints' constants from its layoutConstraint properties.

In your case:

let widthConstant = redViewWidthConstraint?.layoutConstraints[0].constant

All 2 comments

You can always keep a reference to constraints from constraint:

var redViewWidthConstraint: Constraint?
redView.snp_makeConstraints { (make) in
           redViewWidthConstraint = make.width.equalTo(100).constraint
        }

Since a Constraint object has layoutConstraints: [LayoutConstraint], you can get any of its constraints' constants from its layoutConstraint properties.

In your case:

let widthConstant = redViewWidthConstraint?.layoutConstraints[0].constant

@Zsams I recommend @juliensaad's methodology, one thing to be careful with is the order in which the NSLayoutContraint's are installed is not guaranteed so you must take care to check attributes.

Because of that I highly recommend if you need this sort of check you make a Constraint from a single attribute make line such as:

widthConstraint = make.width.equalTo(100).constraint
heightConstraint = make.height.equalTo(100).constraint

This ensures the first object in the array is _always_ the right constraint.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cookiezby picture Cookiezby  路  8Comments

cooler333 picture cooler333  路  5Comments

MoShenGuo picture MoShenGuo  路  4Comments

lolgear picture lolgear  路  5Comments

semiwhale picture semiwhale  路  3Comments