CocoaPods trunk is moving to be read-only. Read more on the blog, there are 16 months to go.

WHCustomizeConstraint 0.1.2

WHCustomizeConstraint 0.1.2

Maintained by Wajahat Hassan.



  • By
  • wajahathassan64

WHCustomizeConstraint

CI Status Version License Platform

Features

  • Swift 5 sweetness.
  • Everything you can do with Auto Layout in shorter way.
  • Constraints are active by default.
  • Compatible with other Auto Layout code.
  • Set constraint priorities upon creation.
  • Constrain directly to the superview.

Installation

WHCustomizeConstraint is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'WHCustomizeConstraint'

Example

To run the example project, clone the repo, and run pod install from the Example directory. Furthermore, some useful examples described below.

Align Edges

Attaching any UI component to its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0),
view.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0),
view.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0),
view.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0)
])

with WHCustomizeConstraint:

view.alignAllEdgesWithSuperview()

or:

view.alignEdgesWithSuperview([.left, .right, .top, .bottom], constants: [0,0,0,0]) 

Center in Superview

Keeping a view (UI-component) to the center of its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
view.centerXAnchor.constraint(equalTo: superview.centerXAnchor, constant: 0)
view.centerYAnchor.constraint(equalTo: superview.centerYAnchor, constant: 0)
])

with WHCustomizeConstraint:

view.centerInSuperView()

Usage

Constraints with Superview

Center Horizontally in Superview

Constraining a view Horizontally to the center of its supperview with WHCustomizeConstraint:

view.centerHorizontallyInSuperview()

Center Vertically in Superview

Constraining a view Vertically to the center of its supperview with WHCustomizeConstraint:

view.centerVerticallyInSuperview()

Edge Superview Safe Area

Attaching a view with superview SafeArea with padding using WHCustomizeConstraint:

view.alignEdgeWithSuperviewSafeArea(.top, constant: 10)

Top to Bottom & Bottom to Top

This constraints the top-anchor of secondView to the bottom-anchor of firstView:

secondView.topToBottom(firstView)
or
secondView.topToBottom(firstView, constant: 10)

This constraints the bottom-anchor of firstView to the top-anchor of secondView:

firstView.bottomToTop(secondView)
or
firstView.bottomToTop(secondView, constant: 10)

Author

wajahathassan64, [email protected]

License

WHCustomizeConstraint is available under the MIT license. See the LICENSE file for more info.