Forked from ha1f/UIViewSafeAreaLayoutGuide+Extension.swift
Last active
November 1, 2017 14:03
-
-
Save mono0926/cca77e34444a04f09f3c7b756dfc4351 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
// https://gist.github.com/ha1f/884ce4870dc395639e86c4395f5ea947 の案1のweak版 | |
extension UIView { | |
/// Returns UILayoutGuide compatible with safeAreaLayoutGuide. | |
/// `.safeAreaLayoutGuide` is available only iOS 11+, but safeAreaLayoutGuide | |
/// should be same as layoutguide of UIView under iOS 11. | |
/// | |
/// - returns safeAreaLayoutGuide or layoutGuide of self | |
func safeLayoutGuideOrSelfLayoutGuide() -> UILayoutGuide { | |
if #available(iOS 11.0, *) { | |
return self.safeAreaLayoutGuide | |
} else { | |
return UIViewLayoutGuideProxy(self) | |
} | |
} | |
} | |
private final class UIViewLayoutGuideProxy: UILayoutGuide { | |
private weak var _base: UIView? | |
init(_ view: UIView) { | |
self._base = view | |
super.init() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("UIViewLayoutGuideProxy cannot be instantiated from NSCoder. Use init(UIView) instead.") | |
} | |
override var leftAnchor: NSLayoutXAxisAnchor { | |
return _base?.leftAnchor ?? NSLayoutXAxisAnchor() | |
} | |
override var topAnchor: NSLayoutYAxisAnchor { | |
return _base?.topAnchor ?? NSLayoutYAxisAnchor() | |
} | |
override var bottomAnchor: NSLayoutYAxisAnchor { | |
return _base?.bottomAnchor ?? NSLayoutYAxisAnchor() | |
} | |
override var rightAnchor: NSLayoutXAxisAnchor { | |
return _base?.rightAnchor ?? NSLayoutXAxisAnchor() | |
} | |
override var widthAnchor: NSLayoutDimension { | |
return _base?.widthAnchor ?? NSLayoutDimension() | |
} | |
override var heightAnchor: NSLayoutDimension { | |
return _base?.heightAnchor ?? NSLayoutDimension() | |
} | |
override var leadingAnchor: NSLayoutXAxisAnchor { | |
return _base?.leadingAnchor ?? NSLayoutXAxisAnchor() | |
} | |
override var trailingAnchor: NSLayoutXAxisAnchor { | |
return _base?.trailingAnchor ?? NSLayoutXAxisAnchor() | |
} | |
override var centerXAnchor: NSLayoutXAxisAnchor { | |
return _base?.centerXAnchor ?? NSLayoutXAxisAnchor() | |
} | |
override var centerYAnchor: NSLayoutYAxisAnchor { | |
return _base?.centerYAnchor ?? NSLayoutYAxisAnchor() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment