Skip to content

Commit

Permalink
Merge pull request #267 from yysskk/master
Browse files Browse the repository at this point in the history
Add Safe Area Layout support
  • Loading branch information
orta authored Oct 18, 2017
2 parents 6b1e875 + 3bff77b commit 6e00426
Show file tree
Hide file tree
Showing 12 changed files with 818 additions and 48 deletions.
8 changes: 8 additions & 0 deletions Cartography.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
BBAC6D141A22A3AC00E8A3E2 /* ViewUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBAC6D121A22A27900E8A3E2 /* ViewUtils.swift */; };
BBAC6D1B1A22A8F300E8A3E2 /* ViewHierarchySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB41A06E1A229BF7007142FE /* ViewHierarchySpec.swift */; };
BBAC6D1C1A22A8F400E8A3E2 /* ViewHierarchySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB41A06E1A229BF7007142FE /* ViewHierarchySpec.swift */; };
EE8531501F936462003EC021 /* LayoutItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE85314F1F9363DC003EC021 /* LayoutItem.swift */; };
EE8531521F93646A003EC021 /* LayoutItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE85314F1F9363DC003EC021 /* LayoutItem.swift */; };
EE8531531F94131B003EC021 /* LayoutItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE85314F1F9363DC003EC021 /* LayoutItem.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -238,6 +241,7 @@
825625A8CCFA9B2B102BDDDB /* Pods_TestPods_Cartography_Mac_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TestPods_Cartography_Mac_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BB41A06E1A229BF7007142FE /* ViewHierarchySpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewHierarchySpec.swift; sourceTree = "<group>"; };
BBAC6D121A22A27900E8A3E2 /* ViewUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewUtils.swift; sourceTree = "<group>"; };
EE85314F1F9363DC003EC021 /* LayoutItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutItem.swift; sourceTree = "<group>"; };
EEDD4098FF7503B1F9188F10 /* Pods_Cartography_iOS_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Cartography_iOS_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -387,6 +391,7 @@
545F858C195322EA00791F75 /* Edges.swift */,
546E9E941950A97F00B16707 /* Expression.swift */,
54B093961A7165F2008A1102 /* Extensions.swift */,
EE85314F1F9363DC003EC021 /* LayoutItem.swift */,
546E9E881950A29300B16707 /* LayoutProxy.swift */,
66AF7EED1CABEABC00249E27 /* LayoutSupport.swift */,
54FA3A3B1951A2B60094B82A /* Point.swift */,
Expand Down Expand Up @@ -873,6 +878,7 @@
54FA3A361950FD320094B82A /* Priority.swift in Sources */,
547BC85719E2EB9B007BEE9E /* Constraint.swift in Sources */,
547BC85519E2DD06007BEE9E /* Context.swift in Sources */,
EE8531501F936462003EC021 /* LayoutItem.swift in Sources */,
545F858D195322EA00791F75 /* Edges.swift in Sources */,
BBAC6D131A22A27900E8A3E2 /* ViewUtils.swift in Sources */,
546E9E8D1950A31100B16707 /* Dimension.swift in Sources */,
Expand Down Expand Up @@ -923,6 +929,7 @@
54F6A858195C213A00313D24 /* LayoutProxy.swift in Sources */,
54BF29B51A9350170066ED10 /* Align.swift in Sources */,
543B37FA1AC82FBF00367D8B /* Edge.swift in Sources */,
EE8531531F94131B003EC021 /* LayoutItem.swift in Sources */,
54F6A853195C213A00313D24 /* Compound.swift in Sources */,
54B093991A716A2E008A1102 /* Extensions.swift in Sources */,
54F6A859195C213A00313D24 /* Priority.swift in Sources */,
Expand Down Expand Up @@ -968,6 +975,7 @@
632F09381BF1F127002431A3 /* Constrain.swift in Sources */,
632F09391BF1F127002431A3 /* Constraint.swift in Sources */,
632F093A1BF1F127002431A3 /* ConstraintGroup.swift in Sources */,
EE8531521F93646A003EC021 /* LayoutItem.swift in Sources */,
632F093B1BF1F127002431A3 /* Context.swift in Sources */,
632F093C1BF1F127002431A3 /* Dimension.swift in Sources */,
632F093D1BF1F127002431A3 /* Distribute.swift in Sources */,
Expand Down
72 changes: 62 additions & 10 deletions Cartography/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ public class Context {

#if os(iOS) || os(tvOS)

internal func addConstraint(_ from: Property, to: LayoutSupport, coefficients: Coefficients = Coefficients(), relation: LayoutRelation = .equal) -> NSLayoutConstraint {
internal func addConstraint(_ from: Property, to: LayoutSupport, coefficients: Coefficients = Coefficients(), relation: LayoutRelation = .equal) -> NSLayoutConstraint {
from.view.car_translatesAutoresizingMaskIntoConstraints = false

let layoutConstraint = NSLayoutConstraint(item: from.view,

let item: LayoutItem
if #available(iOS 11.0, tvOS 11.0, *), from.needsSafeArea {
item = from.view.safeAreaLayoutGuide
} else {
item = from.view
}

let layoutConstraint = NSLayoutConstraint(item: item,
attribute: from.attribute,
relatedBy: relation,
toItem: to.layoutGuide,
attribute: to.attribute,
multiplier: CGFloat(coefficients.multiplier),
constant: CGFloat(coefficients.constant))

var view = from.view
while let superview = view.superview {
view = superview
Expand All @@ -40,17 +47,60 @@ public class Context {

return layoutConstraint
}

#endif


internal func addConstraint(_ from: Property, to: Property? = nil, coefficients: Coefficients = Coefficients(), relation: LayoutRelation = .equal) -> NSLayoutConstraint {

from.view.car_translatesAutoresizingMaskIntoConstraints = false

let item: LayoutItem
let toItem: LayoutItem?

if #available(iOS 11.0, tvOS 11.0, *), from.needsSafeArea {
item = from.view.safeAreaLayoutGuide
} else {
item = from.view
}

if #available(iOS 11.0, tvOS 11.0, *), let needsSafeArea = to?.needsSafeArea, needsSafeArea {
toItem = to?.view.safeAreaLayoutGuide
} else {
toItem = to?.view
}

let layoutConstraint = NSLayoutConstraint(item: item,
attribute: from.attribute,
relatedBy: relation,
toItem: toItem,
attribute: to?.attribute ?? .notAnAttribute,
multiplier: CGFloat(coefficients.multiplier),
constant: CGFloat(coefficients.constant))

if let to = to {
if let common = closestCommonAncestor(from.view, b: to.view ) {
constraints.append(Constraint(view: common, layoutConstraint: layoutConstraint))
} else {
fatalError("No common superview found between \(from.view) and \(to.view)")
}
} else {
constraints.append(Constraint(view: from.view, layoutConstraint: layoutConstraint))
}

return layoutConstraint
}

#else

internal func addConstraint(_ from: Property, to: Property? = nil, coefficients: Coefficients = Coefficients(), relation: LayoutRelation = .equal) -> NSLayoutConstraint {

from.view.car_translatesAutoresizingMaskIntoConstraints = false

let layoutConstraint = NSLayoutConstraint(item: from.view,
let item: LayoutItem = from.view
let toItem: LayoutItem? = to?.view

let layoutConstraint = NSLayoutConstraint(item: item,
attribute: from.attribute,
relatedBy: relation,
toItem: to?.view,
toItem: toItem,
attribute: to?.attribute ?? .notAnAttribute,
multiplier: CGFloat(coefficients.multiplier),
constant: CGFloat(coefficients.constant))
Expand All @@ -68,6 +118,8 @@ public class Context {
return layoutConstraint
}

#endif

internal func addConstraint(_ from: Compound, coefficients: [Coefficients]? = nil, to: Compound? = nil, relation: LayoutRelation = .equal) -> [NSLayoutConstraint] {
var results: [NSLayoutConstraint] = []

Expand Down
4 changes: 3 additions & 1 deletion Cartography/Dimension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public struct Dimension : Property, NumericalEquality, RelativeEquality, Numeric
public let attribute: LayoutAttribute
public let context: Context
public let view: View
public let needsSafeArea: Bool

internal init(_ context: Context, _ view: View, _ attribute: LayoutAttribute) {
internal init(_ context: Context, _ view: View, _ attribute: LayoutAttribute, _ needsSafeArea: Bool) {
self.attribute = attribute
self.context = context
self.view = view
self.needsSafeArea = needsSafeArea
}
}
4 changes: 3 additions & 1 deletion Cartography/Edge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public struct Edge : Property, RelativeEquality, RelativeInequality, Addition, M
public let attribute: LayoutAttribute
public let context: Context
public let view: View
public let needsSafeArea: Bool

internal init(_ context: Context, _ view: View, _ attribute: LayoutAttribute) {
internal init(_ context: Context, _ view: View, _ attribute: LayoutAttribute, _ needsSafeArea: Bool) {
self.attribute = attribute
self.context = context
self.view = view
self.needsSafeArea = needsSafeArea
}
}
21 changes: 21 additions & 0 deletions Cartography/LayoutItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// LayoutItem.swift
// Cartography-iOS
//
// Created by Yusuke Morishia on 2017/10/15.
// Copyright © 2017年 Robert Böhnke. All rights reserved.
//
#if os(iOS) || os(tvOS)
import UIKit
#else
import AppKit
#endif

internal protocol LayoutItem: class {}

extension View: LayoutItem {}

#if os(iOS) || os(tvOS)
@available(iOS 9.0, tvOS 9.0, *)
extension UILayoutGuide: LayoutItem {}
#endif
Loading

0 comments on commit 6e00426

Please sign in to comment.