Skip to content
🧲Mag is Framework which wraps NSLayoutAnchor.
Swift Other
  1. Swift 99.1%
  2. Other 0.9%
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Mag.xcodeproj Delete unnecessary file Jan 20, 2020
Mag Delete unnecessary file Jan 20, 2020
MagExample Update README.md Jan 20, 2020
MagTests Add Operator Jan 20, 2020
Support Files Initial commit Jan 7, 2020
.gitignore Initial commit Jan 7, 2020
.travis.yml Fix os version Jan 7, 2020
LICENSE Initial commit Jan 7, 2020
Mag.podspec Update podspec Jan 20, 2020
README.md
codecov.yml Add codecov Jan 7, 2020

README.md

🧲 Mag

Carthage compatible Build Status codecov Version License Platform

Mag is Framework which wraps NSLayoutAnchor.

Installation

CocoaPods

Add the following to your Podfile:

pod "Mag"

Carthage

Add the following to your Cartfile:

github "cats-oss/Mag"

Example

let redView = UIView(frame: .zero)
redView.backgroundColor = .red
view.addSubview(redView)
redView.sizeAnchor == CGSize(width: 100, height: 100)
redView.leadingAnchor == view.leadingAnchor + 100
redView.topAnchor == (view.bottomAnchor - 100) * 0.5 ~ 999

let label = UILabel(frame: .zero)
label.text = "Example"
label.textAlignment = .center
redView.addSubview(label)
label.edgeAnchor == redView.edgeAnchor

let blueView = UIView(frame: .zero)
blueView.backgroundColor = .blue
view.addSubview(blueView)
blueView.sizeAnchor == redView.sizeAnchor
blueView.centerAnchor == redView.centerAnchor + CGPoint(x: 100, y: -100)

Example

Usage

Alignment

redView.leadingAnchor == view.leadingAnchor + 10
// redView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10)

redView.topAnchor >= view.bottomAnchor
// redView.topAnchor.constraint(greaterThanOrEqualTo: view.bottomAnchor, constant: 0)

redView.centerYAnchor <= view.centerYAnchor - 20
// redView.centerYAnchor.constraint(lessThanOrEqualTo: view.centerYAnchor, constant: -20)

blueView.centerAnchor == redView.centerAnchor + CGPoint(x: 100, y: -100)
// blueView.centerXAnchor.constraint(equalTo: redView.centerXAnchor, constant: 100)
// blueView.centerYAnchor.constraint(equalTo: redView.centerYAnchor, constant: -100)

label.edgeAnchor == redView.edgeAnchor + UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
// label.topAnchor.constraint(equalTo: redView.topAnchor, constant: 5),
// label.bottomAnchor.constraint(equalTo: redView.bottomAnchor, constant: -5)
// label.leadingAnchor.constraint(equalTo: redView.leadingAnchor, constant: 10)
// label.trailingAnchor.constraint(equalTo: redView.trailingAnchor, constant: -10)

redView.topAnchor == view.bottomAnchor * 0.5
// redView.topAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
// multiplying 0.5

Size

redView.widthAnchor == 100
// redView.widthAnchor.constraint(equalToConstant: 100)

redView.heightAnchor >= 200
// redView.heightAnchor.constraint(greaterThanOrEqualToConstant: 200)

redView.widthAnchor <= 200
// redView.widthAnchor.constraint(lessThanOrEqualToConstant: 200)

redView.sizeAnchor == CGSize(width: 100, height: 200)
// redView.widthAnchor.constraint(equalToConstant: 100)
// redView.heightAnchor.constraint(equalToConstant: 200)

blueView.sizeAnchor <= redView.sizeAnchor + CGSize(width: 20, height: 0)
// blueView.widthAnchor.constraint(lessThanOrEqualTo: redView.widthAnchor, constant: 20)
// blueView.heightAnchor.constraint(lessThanOrEqualTo: redView.heightAnchor, constant: 0)

redView.sizeAnchor == view.sizeAnchor * 0.5
// redView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5, constant: 0)
// redView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5, constant: 0)

Priority

redView.topAnchor == view.topAnchor ~ .defaultHigh
// constraint.priority = .defaultHigh

redView.centerAnchor == view.centerAnchor ~ 900
// constraint.priority = UILayoutPriority(Float(900)) (or NSLayoutConstraint.Priority)

Constraints

Comparison operators return NSLayoutConstraint which has already activated. If target anchors are composite anchors, returns multiple NSLayoutConstraint. (e.g. [NSLayoutConstraint])

let constraint = redView.leadingAnchor == view.leadingAnchor
// returns NSLayoutConstraint
constraint.isActive = false

let constraints = redView.centerAnchor == view.centerAnchor
// returns [NSLayoutConstraint]
NSLayoutConstraint.deactivate(constraints)

If firstItem in NSLayoutConstraint is UIView (or NSView), translatesAutoresizingMaskIntoConstraints is set to false.

LICENSE

Under the MIT license. See LICENSE file for details.

You can’t perform that action at this time.