项目作者: jdisho

项目描述 :
🍦 Simplified and chainable AutoLayout constraints for iOS.
高级语言: Swift
项目地址: git://github.com/jdisho/VanillaConstraints.git
创建时间: 2018-08-13T08:48:05Z
项目社区:https://github.com/jdisho/VanillaConstraints

开源协议:MIT License

下载


🍦 VanillaConstraints















Twitter: @_disho


  • A simplified and chainable AutoLayout for iOS written in Swift.
  • A thin wrapper around NSLayoutConstraints.
  • Short syntax for creating layout constraints.
  • Chainable way of describing NSLayoutConstraints.
  • Using KeyPaths.
  • Constraints are active by default.
  • No need to set translatesAutoresizingMaskIntoConstraints = false.
  • No external dependencies.

🛠 Installation

CocoaPods

To integrate VanillaConstraints into your Xcode project using CocoaPods, specify it in your Podfile:

  1. pod 'VanillaConstraints'

Then, run the following command:

  1. $ pod install

Carthage

  1. github "jdisho/VanillaConstraints" ~> 1.0

Then, run the following command:

  1. $ carthage update

Manually

If you prefer not to use any of the dependency managers, you can integrate VanillaConstraints into your project manually, by downloading the source code and placing the files on your project directory.

👨🏻‍💻 Usage

tl;dr

It allows you to replace this:

  1. anotherView.addSubview(view)
  2. view.translatesAutoresizingMaskIntoConstraints = false
  3. let top = view.topAnchor.constraint(equalTo: anotherView.topAnchor, constant: 16.0)
  4. top.priority = .defaultLow
  5. top.isActive = true
  6. let trailing = view.trailingAnchor.constraint(lessThanOrEqualTo: anotherView.trailingAnchor)
  7. trailing.priority = .defaultHigh
  8. trailing.isActive = true
  9. let bottom = view.bottomAnchor.constraint(equalTo: anotherView.bottomAnchor, constant: 16.0)
  10. bottom.priority = .required
  11. bottom.isActive = true
  12. let leading = view.leadingAnchor.constraint(greaterThanOrEqualTo: anotherView.leadingAnchor, constant: 8.0)
  13. leading.isActive = true

with this:

  1. view.add(to: anotherView)
  2. .top(to: \.topAnchor, constant: 16.0, priority: .defaultLow)
  3. .trailing(to: \.trailingAnchor, relation: .equalOrLess, priority: .defaultHigh)
  4. .bottom(to: \.bottomAnchor, constant: 16.0)
  5. .leading(to: \.leadingAnchor, relation: .equalOrGreater, constant: 8.0)

⚠️ If the anchor’s view is not specified, it is constrained where it is added.

Edges

Pin a view to the edges of another view:

  1. anotherView.addSubview(view)
  2. view.translatesAutoresizingMaskIntoConstraints = false
  3. NSLayoutConstraint.activate([
  4. view.topAnchor.constraint(equalTo: anotherView.topAnchor),
  5. view.leadingAnchor.constraint(equalTo: anotherView.leadingAnchor),
  6. view.bottomAnchor.constraint(equalTo: anotherView.bottomAnchor),
  7. view.trailingAnchor.constraint(equalTo: anotherView.trailingAnchor)
  8. ])

with VanillaConstraints:

  1. view.add(to: anotherView).pinToEdges()

or with equal margins:

  1. view.add(to: anotherView).pinToEdges(withMargins: 16.0)

or pinned to some other view different from where it is added:

  1. view.add(to: anotherView).pinToEdges(of: someOtherView)

or pinned to safeAreaLayoutGuide egdes:

  1. view.add(to: anotherView).pinToEdges(safeConstrainable: true) // false by default

Center

Center a view to another view:

  1. anotherView.addSubview(view)
  2. view.translatesAutoresizingMaskIntoConstraints = false
  3. NSLayoutConstraint.activate([
  4. view.centerXAnchor.constraint(equalTo: anotherView.centerXAnchor)
  5. view.centerYAnchor.constraint(equalTo: anotherView.centerYAnchor)
  6. ])

with VanillaConstraints:

  1. view.add(to: anotherView).center()

or centered in some other view different from where it is added:

  1. view.add(to: anotherView).center(in: someOtherView)

Size

Set the size of the view:

  1. anotherView.addSubview(view)
  2. view.translatesAutoresizingMaskIntoConstraints = false
  3. NSLayoutConstraint.activate([
  4. view.widthAnchor.constraint(equalToConstant: 50.0)
  5. view.heightAnchor.constraint(equalToConstant: 50.0)
  6. ])

with VanillaConstraints:

  1. view.add(to: anotherView).size(CGSize(width: 50.0, height: 50))

or with other relations:

  1. view.add(to: anotherView).size(CGSize(width: 50.0, height: 50), relation: .equalOrLess) // .equal by default

Supported attributes

  • top
  • bottom
  • left
  • right
  • leading
  • trailing
  • centerX
  • centerY
  • width
  • height

👤 Author

This tiny library is created with ❤️ by Joan Disho

📃 License

VanillaConstraints is released under an MIT license. See License.md for more information.