Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to get the NSLayoutConstraint when using SnapKit? #133

Closed
NicholasTD07 opened this issue Aug 13, 2015 · 8 comments
Closed

Comments

@NicholasTD07
Copy link

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.

@robertjpayne
Copy link
Member

@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!

@NicholasTD07
Copy link
Author

@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?

@robertjpayne
Copy link
Member

@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?

@robertjpayne
Copy link
Member

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.

@NicholasTD07
Copy link
Author

@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. 👍 👍

@robertjpayne
Copy link
Member

@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!

@NicholasTD07
Copy link
Author

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

@robertjpayne
Copy link
Member

No worries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants