-
Notifications
You must be signed in to change notification settings - Fork 13
/
UIView+SafeAreaCompat.swift
38 lines (34 loc) · 1.31 KB
/
UIView+SafeAreaCompat.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// UIView+SafeAreaCompat.swift
// ACKategories
//
// Created by Jakub Olejník on 06/02/2018.
// Copyright © 2018 Josef Dolezal. All rights reserved.
//
import UIKit
@available(iOS 9.0, *)
extension UIView {
private enum Keys {
static var safeArea: UInt8 = 0
}
/// Layout guide compatibility extension for iOS 11 safe area
///
/// On iOS 11+ is the same as `safeAreaLayoutGuide`.
///
/// On older systems it fallbacks to superview bounds
public var safeArea: UILayoutGuide {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide
} else {
if let layoutGuide = objc_getAssociatedObject(self, &Keys.safeArea) as? UILayoutGuide { return layoutGuide }
let layoutGuide = UILayoutGuide()
addLayoutGuide(layoutGuide)
layoutGuide.topAnchor.constraint(equalTo: topAnchor).isActive = true
layoutGuide.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
layoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
layoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
objc_setAssociatedObject(self, &Keys.safeArea, layoutGuide, .OBJC_ASSOCIATION_ASSIGN)
return layoutGuide
}
}
}