Created
March 9, 2018 11:19
-
-
Save ts95/d409eb28e98b868db77b92647577aa17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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