Skip to content

jduff/Pins

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pins

Build Status

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.

Setup

Carthage

  1. Add the jduff/Pins project to your Cartfile.
github "jduff/Pins"
  1. Run carthage update, then add the framework into your project.
  2. Import the Pins framework/module.
  • Swift: import Pins

Cocoapods

  1. Add the following information to your Podfile.
use_frameworks!

target '<Your Target Name>' do
    pod 'Pins', '~> 1.0'
end
  1. Run pod install
  2. Import the Pins framework/module.
  • Swift: import Pins

Usage

// 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 a View we set translatesAutoresizingMaskIntoConstraints to false so you don't need to.
  • On the NSLayoutConstraint that is created we also set isActive to true so it's already activated for you.
  • We return any NSLayoutConstraint objects that are created when you call pin 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.

License

Pins is licensed under the MIT License. See the LICENSE file for more information.