Skip to content

Instantly share code, notes, and snippets.

@robertjpayne
Last active August 29, 2015 14:20
Show Gist options
  • Save robertjpayne/29016d1af4730d2371b1 to your computer and use it in GitHub Desktop.
Save robertjpayne/29016d1af4730d2371b1 to your computer and use it in GitHub Desktop.
SnapKit Constraint Sheets
@loginViewController {
@formView {
width == ^ - 32.0
centerX == ^
centerY == ^ + 16.0
bottom <= ^ - 16.0
@emailField {
top.left.right == ^
height == 44.0
}
@emailPasswordSeparator {
top == @emailField.bottom
left.right == ^
height == 0.5
}
@passwordField {
top == @emailPasswordSeparator.bottom
left.right.bottom == ^
height == 44.0
}
}
@submit-button {
width == ^ - 32.0
top == @formView.bottom + 16.0
}
}
self.formView.snp_makeConstraints { (make) -> Void in
make.width.equalTo(self.view).offset(-32.0)
make.centerX.equalTo(self.view)
make.centerY.equalTo(self.view).offset(16.0)
make.bottom.lessThanOrEqualTo(self.view).offset(-16.0)
}
self.emailField.snp_makeConstraints { (make) -> Void in
make.top.left.right.equalTo(self.formView)
make.height.equalTo(44.0)
}
self.emailPasswordSeparator.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.emailField.snp_bottom)
make.left.right.equalTo(self.formView)
make.height.equalTo(0.5)
}
self.passwordField.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.emailPasswordSeparator.snp_bottom)
make.bottom.left.right.equalTo(self.formView)
make.height.equalTo(44.0)
}
self.submitButton.snp_makeConstraints { (make) -> Void in
make.width.equalTo(self.formView).offset(-32.0)
make.top.equalTo(self.formView.snp_bottom).offset(16.0)
}
@robertjpayne
Copy link
Author

This is a very early proposal for constraint style sheets for iOS Auto Layout. The main goal is to allow external files to define constraints and be reloaded whenever they are saved.

The idea is that you 'tag' each view:

self.view.snp_tag = "loginViewController"
self.formView.snp_tag = "formView"
// etc…

You would then override updateViewConstraints or updateConstraints:

override func updateViewConstraints() {
     super.updateViewConstraints()
     ConstraintStyleSheet.apply(self, ["paddingAmount": 50.0])
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment