Simple API for Auto Layout interfaces. Works with iOS and macOS.
Pins takes care of the mundane and simplifies the code you need to write whenever possible. You don't need to set translatesAutoresizingMaskIntoConstraints
to false
all over the place, calling pin
will do that for you. You probably want the constraints you just created to be activated so Pins will do that for you too. Needing to negate padding when creating constraints to right
, bottom
or trailing
anchors isn't intuitive so pins will take care of that. If Pins doesn't do everything you need or you need to reference the constraints later no problem, they are returned whenever you call pin
so you can have full control.
- Add the jduff/Pins project to your
Cartfile
.
github "jduff/Pins"
- Run
carthage update
, then add the framework into your project. - Import the Pins framework/module.
- Swift:
import Pins
- Add the following information to your
Podfile
.
use_frameworks!
target '<Your Target Name>' do
pod 'Pins', '~> 1.0'
end
- Run
pod install
- Import the Pins framework/module.
- Swift:
import Pins
// Pin bounds, 4 constraints created and activated
nestedView.pin(leadingTo: mainView.leftAnchor, topTo: mainView.topAnchor, trailingTo: mainView.rightAnchor, bottomTo: mainView.bottomAnchor)
nestedView.pin(to: mainView) // same as above
// Pins are optional when it makes sense, 2 constraints created and activated
nestedView.pin(leadingTo: mainView.leftAnchor, topTo: nil, trailingTo: mainView.rightAnchor, bottomTo: nil)
// Pin height and width, 2 constraints created and activated
nestedView.pin(height: 20, width: 10)
// Pin top with padding, 1 constraint with constant of 10 created and activated
nestedView.pin(.top, to: mainView.topAnchor, padding: 10)
let topConstraint = nestedView.pin(.top, to: mainView, padding: 10) // same as above
// Pin to UILayout guides
let space = UILayoutGuide()
mainView.addLayoutGuide(space)
nestedView.pin(.right, to: space.leftAnchor, padding: 10)
// Animate a constraint
let top = nestedView.pin(.top, to: mainView) // returns a NSLayoutConstraint that you can use later on
top.constant = 30
UIView.animateWithDuration(0.3) {
self.layoutIfNeeded()
}
- When you call
pin
on aView
we settranslatesAutoresizingMaskIntoConstraints
tofalse
so you don't need to. - On the
NSLayoutConstraint
that is created we also setisActive
totrue
so it's already activated for you. - We return any
NSLayoutConstraint
objects that are created when you callpin
so that you can reference them later or do anything else you might need. - For simplicity, the padding value will be appropriately negated when applied to right/trailing/bottom edges. If you wish to push views further to the right, use a negative padding value.
Pins is licensed under the MIT License. See the LICENSE file for more information.