Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Created October 31, 2017 11:57
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IanKeen/aee3a8b4667309a95e2fc47ac3ee6ca6 to your computer and use it in GitHub Desktop.
Save IanKeen/aee3a8b4667309a95e2fc47ac3ee6ca6 to your computer and use it in GitHub Desktop.
Extension for constraints pointing to either the view or safeAreaLayoutGuide depending on availability
protocol LayoutGuideProvider {
var leadingAnchor: NSLayoutXAxisAnchor { get }
var trailingAnchor: NSLayoutXAxisAnchor { get }
var leftAnchor: NSLayoutXAxisAnchor { get }
var rightAnchor: NSLayoutXAxisAnchor { get }
var topAnchor: NSLayoutYAxisAnchor { get }
var bottomAnchor: NSLayoutYAxisAnchor { get }
var widthAnchor: NSLayoutDimension { get }
var heightAnchor: NSLayoutDimension { get }
var centerXAnchor: NSLayoutXAxisAnchor { get }
var centerYAnchor: NSLayoutYAxisAnchor { get }
}
extension UIView: LayoutGuideProvider { }
extension UILayoutGuide: LayoutGuideProvider { }
extension UIView {
var compatibleSafeAreaLayoutGuide: LayoutGuideProvider {
if #available(iOS 11, *) {
return safeAreaLayoutGuide
} else {
return self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment