Skip to content

Instantly share code, notes, and snippets.

View pedrommcarrasco's full-sized avatar

Pedro Carrasco pedrommcarrasco

View GitHub Profile
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:20
Power-Up Your Anchors - Adding to the view hierarchy (before)
[logoImageView, welcomeLabel, dismissButton].forEach {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:20
Power-Up Your Anchors - Adding to the view hierarchy (after)
addSubviewsUsingAutoLayout(logoImageView, welcomeLabel, dismissButton)
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:21
Power-Up Your Anchors - NSLayoutAnchor (start)
extension NSLayoutAnchor {
func test() {}
}
@objc extension NSLayoutAnchor {
func test() {}
}
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:24
Power-Up Your Anchors - NSLayoutAnchor (anchor, constant, isActive)
@objc extension NSLayoutAnchor {
@discardableResult
func constrain(equalTo anchor: NSLayoutAnchor,
with constant: CGFloat = 0.0,
isActive: Bool = true) -> NSLayoutConstraint {
let constraint = self.constraint(equalTo: anchor, constant: constant)
constraint.isActive = isActive
return constraint
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:26
Power-Up Your Anchors - NSLayoutAnchor (relation, anchor, constant, isActive)
@objc extension NSLayoutAnchor {
@discardableResult
func constrain(_ relation: NSLayoutConstraint.Relation = .equal,
to anchor: NSLayoutAnchor,
with constant: CGFloat = 0.0,
isActive: Bool = true) -> NSLayoutConstraint {
let constraint: NSLayoutConstraint
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:26
Power-Up Your Anchors - Usage example (NSLayoutAnchor only)
let a = UIView()
let b = UIView()
a.addSubviewsUsingAutoLayout(b)
// Constraint set as equal to a.topAnchor
b.topAnchor.constrain(a.topAnchor)
// Constraint set as greater than or equal to a.bottomAnchor
b.bottomAnchor.constrain(.greaterThanOrEqual, to: a.bottomAnchor)
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Last active January 11, 2019 13:34
Power-Up Your Anchors - NSLayoutDimension
extension NSLayoutDimension {
@discardableResult
func constrain(_ relation: NSLayoutConstraint.Relation = .equal,
to anchor: NSLayoutDimension,
with constant: CGFloat = 0.0,
multiplyBy multiplier: CGFloat = 1.0,
isActive: Bool = true) -> NSLayoutConstraint {
let constraint: NSLayoutConstraint
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Last active January 6, 2019 20:29
Power-Up Your Anchors - Using priorities (before)
let bWidth = b.widthAnchor.constraint(equalToConstant: 50.0)
bWidth.priority = .defaultHigh
// or if you want between .defaultHigh and .required 👇 🤮
bWidth.priority = UILayoutPriority(UILayoutPriority.defaultHigh.rawValue + 1)
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Last active January 11, 2019 13:34
Power-Up Your Anchors - Updates all with priorities
@objc extension NSLayoutAnchor {
@discardableResult
func constrain(_ relation: NSLayoutConstraint.Relation = .equal,
to anchor: NSLayoutAnchor,
with constant: CGFloat = 0.0,
prioritizeAs priority: UILayoutPriority = .required,
isActive: Bool = true) -> NSLayoutConstraint {
let constraint: NSLayoutConstraint