Skip to content

Commit

Permalink
Add safeAreaLayoutGuide support for iOS versions below 11.
Browse files Browse the repository at this point in the history
  • Loading branch information
srdanrasic committed May 2, 2018
1 parent 28fd717 commit 471f372
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Layoutless.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = "Layoutless"
s.version = "0.1.3"
s.version = "0.1.4"
s.summary = "Write less UI code"
s.description = "Layoutless enables you to spend less time writing UI code. It provides a way to abstract common layout patterns and enable consistent styling approach."
s.homepage = "https://github.com/DeclarativeHub/Layoutless"
s.license = 'MIT'
s.author = { "Srdan Rasic" => "srdan.rasic@gmail.com" }
s.source = { :git => "https://github.com/DeclarativeHub/Layoutless.git", :tag => "0.1.3" }
s.source = { :git => "https://github.com/DeclarativeHub/Layoutless.git", :tag => "0.1.4" }

s.ios.deployment_target = '9.3'
s.tvos.deployment_target = '11.0'
Expand Down
6 changes: 6 additions & 0 deletions Layoutless.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
EC2870F0201E448100702624 /* Layout.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC2870EF201E448100702624 /* Layout.swift */; };
EC2870F2201E44CE00702624 /* Layoutless+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC2870F1201E44CE00702624 /* Layoutless+UIKit.swift */; };
EC2870F4201E455A00702624 /* LayoutNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC2870F3201E455A00702624 /* LayoutNode.swift */; };
EC8287712099D56F00E371EC /* UIView+iOS10.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC8287702099D56F00E371EC /* UIView+iOS10.swift */; };
EC8287722099D56F00E371EC /* UIView+iOS10.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC8287702099D56F00E371EC /* UIView+iOS10.swift */; };
ECCF2CE420246CD9008099DD /* Layoutless.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECCF2CE320246CD9008099DD /* Layoutless.swift */; };
ECCF2CE620246DF0008099DD /* ChildNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECCF2CE520246DF0008099DD /* ChildNode.swift */; };
ECCF2D9D2024C03A008099DD /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECCF2D8D2024C03A008099DD /* Label.swift */; };
Expand Down Expand Up @@ -57,6 +59,7 @@
EC2870EF201E448100702624 /* Layout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Layout.swift; sourceTree = "<group>"; };
EC2870F1201E44CE00702624 /* Layoutless+UIKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Layoutless+UIKit.swift"; sourceTree = "<group>"; };
EC2870F3201E455A00702624 /* LayoutNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutNode.swift; sourceTree = "<group>"; };
EC8287702099D56F00E371EC /* UIView+iOS10.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+iOS10.swift"; sourceTree = "<group>"; };
ECCF2CE320246CD9008099DD /* Layoutless.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Layoutless.swift; sourceTree = "<group>"; };
ECCF2CE520246DF0008099DD /* ChildNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChildNode.swift; sourceTree = "<group>"; };
ECCF2D8D2024C03A008099DD /* Label.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -141,6 +144,7 @@
ECCF2CE520246DF0008099DD /* ChildNode.swift */,
ECCF2CE320246CD9008099DD /* Layoutless.swift */,
EC2870F1201E44CE00702624 /* Layoutless+UIKit.swift */,
EC8287702099D56F00E371EC /* UIView+iOS10.swift */,
);
path = Layout;
sourceTree = "<group>";
Expand Down Expand Up @@ -300,6 +304,7 @@
EC2870EE201E446900702624 /* LayoutProtocol.swift in Sources */,
ECCF2DA22024C03A008099DD /* TextField.swift in Sources */,
ECCF2DA32024C03A008099DD /* ImageView.swift in Sources */,
EC8287712099D56F00E371EC /* UIView+iOS10.swift in Sources */,
ECCF2D9D2024C03A008099DD /* Label.swift in Sources */,
EC2870F4201E455A00702624 /* LayoutNode.swift in Sources */,
ECCF2D9E2024C03A008099DD /* ViewController.swift in Sources */,
Expand All @@ -325,6 +330,7 @@
ECCF2E0F20274F82008099DD /* Anchorable.swift in Sources */,
ECCF2E0E20274F82008099DD /* Length.swift in Sources */,
ECCF2E1220274F82008099DD /* Layout.swift in Sources */,
EC8287722099D56F00E371EC /* UIView+iOS10.swift in Sources */,
ECCF2E1020274F82008099DD /* LayoutNode.swift in Sources */,
ECCF2E1820274F88008099DD /* Label.swift in Sources */,
ECCF2E1A20274F88008099DD /* TextField.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Layoutless/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.1.3</string>
<string>0.1.4</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Layout/Layoutless.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension LayoutProtocol where LayoutNode: Anchorable {
if #available(iOS 11.0, *), safeArea {
layout(parent.safeAreaLayoutGuide, node)
} else {
layout(parent, node)
layout(parent.___safeAreaLayoutGuide, node)
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions Sources/Layout/UIView+iOS10.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// UIView+iOS10.swift
// Layoutless
//
// Created by Srdan Rasic on 02/05/2018.
// Copyright © 2018 Declarative Hub. All rights reserved.
//

import UIKit

extension UIView {

private struct AssociatedKeys {
static var safeAreaLayoutGuide = "safeAreaLayoutGuide"
}

internal var ___safeAreaLayoutGuide: UILayoutGuide {
get {
let layoutGuide = objc_getAssociatedObject(self, &AssociatedKeys.safeAreaLayoutGuide) as? UILayoutGuide
if let layoutGuide = layoutGuide {
return layoutGuide
} else {
let layoutGuide = UILayoutGuide()
addLayoutGuide(layoutGuide)
NSLayoutConstraint.activate([
layoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor),
layoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)
])
objc_setAssociatedObject(self, &AssociatedKeys.safeAreaLayoutGuide, layoutGuide, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return layoutGuide
}
}
}
}
15 changes: 15 additions & 0 deletions Sources/Views/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ open class View: UIView {
open var subviewsLayout: AnyLayout {
return EmptyLayout()
}

open override func didMoveToSuperview() {
super.didMoveToSuperview()

guard let superview = superview else { return }

if #available(iOS 11.0, *) {
// safeAreaLayoutGuide is already available
} else {
NSLayoutConstraint.activate([
___safeAreaLayoutGuide.topAnchor.constraint(equalTo: superview.___safeAreaLayoutGuide.topAnchor),
___safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: superview.___safeAreaLayoutGuide.bottomAnchor)
])
}
}
}
14 changes: 14 additions & 0 deletions Sources/Views/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ open class ViewController: UIViewController {

open override func viewDidLoad() {
super.viewDidLoad()

if #available(iOS 11.0, *) {
// safeAreaLayoutGuide is already available
} else {
setupSafeAreaLayoutGuideFallback()
}

defineLayout()
}

Expand All @@ -51,4 +58,11 @@ open class ViewController: UIViewController {
open var subviewsLayout: AnyLayout {
return EmptyLayout()
}

private func setupSafeAreaLayoutGuideFallback() {
NSLayoutConstraint.activate([
view.___safeAreaLayoutGuide.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor),
view.___safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor)
])
}
}

0 comments on commit 471f372

Please sign in to comment.