Skip to content

Commit

Permalink
Merge pull request #269 from corujautx/master
Browse files Browse the repository at this point in the history
Adding support to UILayoutGuide
  • Loading branch information
orta authored Oct 31, 2017
2 parents 6e00426 + fc476fe commit bc78595
Show file tree
Hide file tree
Showing 33 changed files with 1,380 additions and 1,479 deletions.
2 changes: 1 addition & 1 deletion Cartography.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Cartography"
s.version = "2.0.0"
s.version = "3.0.0"
s.summary = "Declarative Auto Layout in Swift"

s.description = <<-DESC
Expand Down
306 changes: 177 additions & 129 deletions Cartography.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

186 changes: 95 additions & 91 deletions Cartography/Align.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,234 +12,238 @@ import UIKit
import AppKit
#endif

private func makeEqual<P: RelativeEquality>(by attribute: (LayoutProxy) -> P, elements: [LayoutProxy]) -> [NSLayoutConstraint] {
if let first = elements.first {
first.view.car_translatesAutoresizingMaskIntoConstraints = false

let rest = elements.dropFirst()
private func makeEqual<P: RelativeEquality, T: LayoutProxy>(by attribute: (T) -> P, items: [T]) -> [NSLayoutConstraint] {
if let first = items.first {
if let first = first as? AutoresizingMaskLayoutProxy {
first.translatesAutoresizingMaskIntoConstraints = false
}

let rest = items.dropFirst()

return rest.reduce([]) { acc, current in
current.view.car_translatesAutoresizingMaskIntoConstraints = false

if let current = current as? AutoresizingMaskLayoutProxy {
current.translatesAutoresizingMaskIntoConstraints = false
}

return acc + [ attribute(first) == attribute(current) ]
}
} else {
return []
}
}

/// Aligns multiple views by their top edge.
/// Aligns multiple items by their top edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(top views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.top }, elements: views)
@discardableResult public func align(top items: [SupportsTopLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.top }, items: items.map(AnyTopLayoutProxy.init))
}

/// Aligns multiple views by their right edge.
/// Aligns multiple items by their top edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(right views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.right }, elements: views)
@discardableResult public func align(top first: SupportsTopLayoutProxy, _ rest: SupportsTopLayoutProxy...) -> [NSLayoutConstraint] {
return align(top: [first] + rest)
}

/// Aligns multiple views by their bottom edge.
/// Aligns multiple items by their right edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(bottom views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.bottom }, elements: views)
@discardableResult public func align(right items: [SupportsRightLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.right }, items: items.map(AnyRightLayoutProxy.init))
}

/// Aligns multiple views by their left edge.
/// Aligns multiple items by their right edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(left views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.left }, elements: views)
@discardableResult public func align(right first: SupportsRightLayoutProxy, _ rest: SupportsRightLayoutProxy...) -> [NSLayoutConstraint] {
return align(right: [first] + rest)
}

/// Aligns multiple views by their leading edge.
/// Aligns multiple items by their bottom edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(leading views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.leading }, elements: views)
@discardableResult public func align(bottom items: [SupportsBottomLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.bottom }, items: items.map(AnyBottomLayoutProxy.init))
}

/// Aligns multiple views by their trailing edge.
/// Aligns multiple items by their bottom edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(trailing views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.trailing }, elements: views)
@discardableResult public func align(bottom first: SupportsBottomLayoutProxy, _ rest: SupportsBottomLayoutProxy...) -> [NSLayoutConstraint] {
return align(bottom: [first] + rest)
}

/// Aligns multiple views by their horizontal center.
/// Aligns multiple items by their left edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(centerX views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.centerX }, elements: views)
@discardableResult public func align(left items: [SupportsLeftLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.left }, items: items.map(AnyLeftLayoutProxy.init))
}

/// Aligns multiple views by their vertical center.
/// Aligns multiple items by their left edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(centerY views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.centerY }, elements: views)
@discardableResult public func align(left first: SupportsLeftLayoutProxy, _ rest: SupportsLeftLayoutProxy...) -> [NSLayoutConstraint] {
return align(left: [first] + rest)
}

/// Aligns multiple views by their baseline.
/// Aligns multiple items by their leading edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter views: an array of views to align
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(baseline views: [LayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.baseline }, elements: views)
@discardableResult public func align(leading items: [SupportsLeadingLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.leading }, items: items.map(AnyLeadingLayoutProxy.init))
}

/// Aligns multiple views by their top edge.
/// Aligns multiple items by their leading edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(top first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(top: [first] + rest)
@discardableResult public func align(leading first: SupportsLeadingLayoutProxy, _ rest: SupportsLeadingLayoutProxy...) -> [NSLayoutConstraint] {
return align(leading: [first] + rest)
}

/// Aligns multiple views by their right edge.
/// Aligns multiple items by their trailing edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(right first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(right: [first] + rest)
@discardableResult public func align(trailing items: [SupportsTrailingLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.trailing }, items: items.map(AnyTrailingLayoutProxy.init))
}

/// Aligns multiple views by their bottom edge.
/// Aligns multiple vies by their trailing edge.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(bottom first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(bottom: [first] + rest)
@discardableResult public func align(trailing first: SupportsTrailingLayoutProxy, _ rest: SupportsTrailingLayoutProxy...) -> [NSLayoutConstraint] {
return align(trailing: [first] + rest)
}

/// Aligns multiple views by their left edge.
/// Aligns multiple items by their horizontal center.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(left first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(left: [first] + rest)
@discardableResult public func align(centerX items: [SupportsCenterXLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.centerX }, items: items.map(AnyCenterXLayoutProxy.init))
}

/// Aligns multiple views by their leading edge.
/// Aligns multiple items by their horizontal center.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(leading first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(leading: [first] + rest)
@discardableResult public func align(centerX first: SupportsCenterXLayoutProxy, _ rest: SupportsCenterXLayoutProxy...) -> [NSLayoutConstraint] {
return align(centerX: [first] + rest)
}

/// Aligns multiple vies by their trailing edge.
/// Aligns multiple items by their vertical center.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(trailing first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(trailing: [first] + rest)
@discardableResult public func align(centerY items: [SupportsCenterYLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.centerY }, items: items.map(AnyCenterYLayoutProxy.init))
}

/// Aligns multiple views by their horizontal center.
/// Aligns multiple items by their vertical center.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(centerX first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(centerX: [first] + rest)
@discardableResult public func align(centerY first: SupportsCenterYLayoutProxy, _ rest: SupportsCenterYLayoutProxy...) -> [NSLayoutConstraint] {
return align(centerY: [first] + rest)
}

/// Aligns multiple views by their vertical center.
/// Aligns multiple items by their baseline.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - parameter items: an array of items to align
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(centerY first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
return align(centerY: [first] + rest)
@discardableResult public func align(baseline items: [SupportsBaselineLayoutProxy]) -> [NSLayoutConstraint] {
return makeEqual(by: { $0.baseline }, items: items.map(AnyBaselineLayoutProxy.init))
}

/// Aligns multiple views by their baseline.
/// Aligns multiple items by their baseline.
///
/// All views passed to this function will have
/// All items passed to this function will have
/// their `translatesAutoresizingMaskIntoConstraints` properties set to `false`.
///
/// - returns: An array of `NSLayoutConstraint` instances.
///
@discardableResult public func align(baseline first: LayoutProxy, _ rest: LayoutProxy...) -> [NSLayoutConstraint] {
@discardableResult public func align(baseline first: SupportsBaselineLayoutProxy, _ rest: SupportsBaselineLayoutProxy...) -> [NSLayoutConstraint] {
return align(baseline: [first] + rest)
}
13 changes: 13 additions & 0 deletions Cartography/AutoresizingMaskLayoutProxy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// AutoresizingMaskLayoutProxy.swift
// Cartography-iOS
//
// Created by Vitor Travain on 24/10/17.
// Copyright © 2017 Robert Böhnke. All rights reserved.
//

import Foundation

public protocol AutoresizingMaskLayoutProxy: LayoutProxy {
var translatesAutoresizingMaskIntoConstraints: Bool { get set }
}
Loading

0 comments on commit bc78595

Please sign in to comment.