Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to UILayoutGuide #269

Merged
merged 20 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2732065
Adding support to UILayoutGuide in Cartography
corujautx Oct 11, 2017
462273b
Removing elements from constrain method signature
corujautx Oct 16, 2017
6bcf956
Adding tests for LayoutGuide support
corujautx Oct 16, 2017
6421a8c
Adding convenience support to safeAreaLayoutGuide
corujautx Oct 16, 2017
6729905
Fixing documentation replacing references to view
corujautx Oct 16, 2017
a91c01a
Merge pull request #1 from corujautx/feature/layout-guide-support
corujautx Oct 16, 2017
b6eb9c9
Changing access control of proxy properties
corujautx Oct 16, 2017
e48af31
Fixing issues with macOS
corujautx Oct 16, 2017
bcf9c93
Fixing issue with top margin
corujautx Oct 16, 2017
a31a7e2
Removing baseline properties from layout guide proxy
corujautx Oct 17, 2017
878f609
Removing IUOs & adding type-erasure
corujautx Oct 17, 2017
1bb14b9
Adding safeAreaLayoutGuide tests
corujautx Oct 17, 2017
e4585a3
Renaming `LayoutElement` to `LayoutItem`
corujautx Oct 17, 2017
f2a8b73
Fixing type-erasures not being linked for the macOS target
corujautx Oct 17, 2017
767821e
Merge branch 'master' of https://github.com/robb/Cartography
corujautx Oct 18, 2017
5cd25b2
Fixing autoresizing mask to match older behavior
corujautx Oct 24, 2017
4cdc8fa
Fixing generation of view proxies
corujautx Oct 24, 2017
143d9fc
Fixing issue with Context not disabling autoresizing mask translation
corujautx Oct 24, 2017
da5e483
Changing podspec version to 3.0.0
corujautx Oct 24, 2017
fc476fe
Adding AutoresizingMaskLayoutProxy to macOS and tvOS targets
corujautx Oct 24, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 108 additions & 18 deletions Cartography.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

178 changes: 87 additions & 91 deletions Cartography/Align.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,234 +12,230 @@ 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 {
let rest = items.dropFirst()

return rest.reduce([]) { acc, current in
current.view.car_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)
}
Loading