Skip to content

Instantly share code, notes, and snippets.

@ts95
Created March 9, 2018 11:19
Show Gist options
  • Save ts95/d409eb28e98b868db77b92647577aa17 to your computer and use it in GitHub Desktop.
Save ts95/d409eb28e98b868db77b92647577aa17 to your computer and use it in GitHub Desktop.
//
// AgnosticSafeArea.swift
//
import UIKit
extension UIViewController {
/**
Auxiliary version-agnostic safe area layout guide
*/
var safeLayoutGuide: UILayoutGuide {
if #available(iOS 11.0, *) {
return view.safeAreaLayoutGuide
} else {
let identifier = "safeAreaGuide"
if let layoutGuide = view.layoutGuides.first(where: { $0.identifier == identifier }) {
return layoutGuide
} else {
let layoutGuide = UILayoutGuide()
layoutGuide.identifier = identifier
view.addLayoutGuide(layoutGuide)
NSLayoutConstraint.activate([
layoutGuide.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor),
layoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
layoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),
layoutGuide.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor)
])
return layoutGuide
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment