@@ -14,6 +14,8 @@ public extension UIView {
14
14
static var keyboardLayoutGuide = " keyboardLayoutGuide "
15
15
}
16
16
17
+ /// A layout guide representing the inset for the keyboard.
18
+ /// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard.
17
19
public var keyboardLayoutGuide : KeyboardLayoutGuide {
18
20
get {
19
21
if let obj = objc_getAssociatedObject ( self , & AssociatedKeys. keyboardLayoutGuide) as? KeyboardLayoutGuide {
@@ -28,9 +30,7 @@ public extension UIView {
28
30
}
29
31
}
30
32
31
- public class KeyboardLayoutGuide : UILayoutGuide {
32
-
33
- private var token : NSKeyValueObservation ? = nil
33
+ open class KeyboardLayoutGuide : UILayoutGuide {
34
34
35
35
public required init ? ( coder aDecoder: NSCoder ) {
36
36
fatalError ( " init(coder:) has not been implemented " )
@@ -51,10 +51,12 @@ public class KeyboardLayoutGuide: UILayoutGuide {
51
51
guard let view = owningView else {
52
52
return
53
53
}
54
- heightAnchor. constraint ( equalToConstant: 0 ) . isActive = true
55
- leftAnchor. constraint ( equalTo: view. leftAnchor) . isActive = true
56
- rightAnchor. constraint ( equalTo: view. rightAnchor) . isActive = true
57
- let viewBottomAnchor : NSLayoutYAxisAnchor !
54
+ NSLayoutConstraint . activate ( [
55
+ heightAnchor. constraint ( equalToConstant: 0 ) ,
56
+ leftAnchor. constraint ( equalTo: view. leftAnchor) ,
57
+ rightAnchor. constraint ( equalTo: view. rightAnchor) ,
58
+ ] )
59
+ let viewBottomAnchor : NSLayoutYAxisAnchor
58
60
if #available( iOS 11 . 0 , * ) {
59
61
viewBottomAnchor = view. safeAreaLayoutGuide. bottomAnchor
60
62
} else {
@@ -64,7 +66,7 @@ public class KeyboardLayoutGuide: UILayoutGuide {
64
66
}
65
67
66
68
@objc
67
- func keyboardWillChangeFrame( _ note: Notification ) {
69
+ private func keyboardWillChangeFrame( _ note: Notification ) {
68
70
if var height = note. keyboardHeight {
69
71
if #available( iOS 11 . 0 , * ) , height > 0 {
70
72
height -= ( owningView? . safeAreaInsets. bottom) !
@@ -92,7 +94,7 @@ public class KeyboardLayoutGuide: UILayoutGuide {
92
94
// MARK: - Helpers
93
95
94
96
extension UILayoutGuide {
95
- var heightConstraint : NSLayoutConstraint ? {
97
+ internal var heightConstraint : NSLayoutConstraint ? {
96
98
guard let target = owningView else { return nil }
97
99
for c in target. constraints {
98
100
if let fi = c. firstItem as? UILayoutGuide , fi == self && c. firstAttribute == . height {
0 commit comments