Snapkit: Is it possible to get the NSLayoutConstraint when using SnapKit?

Created on 13 Aug 2015  路  8Comments  路  Source: SnapKit/SnapKit

I know I can get SnapKit.Constraint from SnapKit by doing topConstraint = make.top.equalTo(superview).offset(padding.top).constraint.

However, is there a way to get the underlining NSLayoutConstraint? I need to get the constraint to work with AMScrollingNavbar.

For example, let's say I have

let vc = UIViewController()
let container = UIView()
vc.view = UIView(frame: CGRectMake(0, 0, 300, 300))

vc.view.addSubview(container)

container.snp_makeConstraints { (make) -> Void in
    make.top.equalTo(vc.snp_topLayoutGuideBottom) // Hopefully I can get the `NSLayoutConstraint` created by this line
    make.bottom.equalTo(vc.snp_bottomLayoutGuideTop)
}

Is it possible to get the NSLayoutConstraint for the top constraint of the container in vc?

PS.
SnapKit is awesome! It makes my life with AutoLayout so much easier. ;D
Thanks for the great work.

Most helpful comment

@NicholasTD07 you probably don't want to store the NSLayoutConstraint since SnapKit will uninstall and re-install these (sometimes creating new ones) as necessary.

Instead you want to store the SnapKit Constraint object:

self.storedConstraint = make.top.equalTo(other).constraint

You can then find methods on that constraint!

All 8 comments

@NicholasTD07 you probably don't want to store the NSLayoutConstraint since SnapKit will uninstall and re-install these (sometimes creating new ones) as necessary.

Instead you want to store the SnapKit Constraint object:

self.storedConstraint = make.top.equalTo(other).constraint

You can then find methods on that constraint!

@robertjpayne Oh, right. Sorry I have only been using SnapKit for a few days so I keep forgetting SnapKit manage(install/re-install) NSLayoutConstraint.

But, in this case, when I do need to hold onto a NSLayoutConstraint, maybe I shouldn't use SnapKit for that particular constraint. Would using NSLayoutConstraint directly while using SnapKit mess up SnapKit itself? Is it possible to use them at the same time?

@NicholasTD07 what's the reasoning for needing the NSLayoutConstraint directly? If you look at the methods available on Constraint here https://github.com/SnapKit/SnapKit/blob/develop/Source/Constraint.swift it should be everything above and beyond you would need?

You can mix regular constraints into SnapKit too, no conflict so long as the constraints are still satisfiable. SnapKit will _never_ touch non SnapKit constraints with the exception of turning off automatic autoresizing flag constraints.

@robertjpayne If you look into AMScrollingNavbar's README there's a section where you can use it with a topConstraint: NSLayoutConstraint. I would like to use it with SnapKit. However, it seems I have to do the topConstraint myself rather than asking SnapKit's help.

By the way, SnapKit is so cool! And the code is so clean. Good job. :+1: :+1:

@NicholasTD07 ah yea, basically SnapKit automagically removes and manages any NSLayoutConstraint's it creates so it's not easy to try and get them directly. You're best creating that one specifically via the native apis!

@robertjpayne Cool. Thank you for helping me understand the internal mechanism of SnapKit and find the solution(native API).

No worries!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

semiwhale picture semiwhale  路  3Comments

seljabali picture seljabali  路  3Comments

MoShenGuo picture MoShenGuo  路  4Comments

lolgear picture lolgear  路  5Comments

romk1n picture romk1n  路  3Comments