Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marianoabdala/2e873480646d3f85d1aae21813554658 to your computer and use it in GitHub Desktop.
Save marianoabdala/2e873480646d3f85d1aae21813554658 to your computer and use it in GitHub Desktop.
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