Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backwards compatible Safe Area Layout Guide API for iOS 10 and iOS 9
import UIKit
extension UIViewController {
var ft_safeAreaLayoutGuide: UILayoutGuide {
if #available(iOS 11.0, *) {
return view.safeAreaLayoutGuide
} else {
let id = "ft_safeAreaLayoutGuide"
if let layoutGuide = view.layoutGuides.filter({ $0.identifier == id }).first {
return layoutGuide
} else {
let layoutGuide = UILayoutGuide()
layoutGuide.identifier = id
view.addLayoutGuide(layoutGuide)
layoutGuide.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
layoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
layoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
layoutGuide.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true
return layoutGuide
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment