@@ -17,7 +17,7 @@ extension UIView {
1717 private enum AssociatedKeys {
1818 static var keyboardLayoutGuide = " keyboardLayoutGuide "
1919 }
20-
20+
2121 /// A layout guide representing the inset for the keyboard.
2222 /// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard.
2323 public var keyboardLayoutGuide : KeyboardLayoutGuide {
@@ -33,11 +33,19 @@ extension UIView {
3333}
3434
3535open class KeyboardLayoutGuide : UILayoutGuide {
36+ public var usesSafeArea = true {
37+ didSet {
38+ updateButtomAnchor ( )
39+ }
40+ }
41+
42+ private var bottomConstraint : NSLayoutConstraint ?
43+
3644 @available ( * , unavailable)
3745 public required init ? ( coder aDecoder: NSCoder ) {
3846 fatalError ( " init(coder:) has not been implemented " )
3947 }
40-
48+
4149 public init ( notificationCenter: NotificationCenter = NotificationCenter . default) {
4250 super. init ( )
4351 // Observe keyboardWillChangeFrame notifications
@@ -48,7 +56,7 @@ open class KeyboardLayoutGuide: UILayoutGuide {
4856 object: nil
4957 )
5058 }
51-
59+
5260 internal func setUp( ) {
5361 guard let view = owningView else { return }
5462 NSLayoutConstraint . activate (
@@ -58,27 +66,39 @@ open class KeyboardLayoutGuide: UILayoutGuide {
5866 rightAnchor. constraint ( equalTo: view. rightAnchor) ,
5967 ]
6068 )
69+ updateButtomAnchor ( )
70+ }
71+
72+ func updateButtomAnchor( ) {
73+ if let bottomConstraint = bottomConstraint {
74+ bottomConstraint. isActive = false
75+ }
76+
77+ guard let view = owningView else { return }
78+
6179 let viewBottomAnchor : NSLayoutYAxisAnchor
62- if #available( iOS 11 . 0 , * ) {
80+ if #available( iOS 11 . 0 , * ) , usesSafeArea {
6381 viewBottomAnchor = view. safeAreaLayoutGuide. bottomAnchor
6482 } else {
6583 viewBottomAnchor = view. bottomAnchor
6684 }
67- bottomAnchor. constraint ( equalTo: viewBottomAnchor) . isActive = true
85+
86+ bottomConstraint = bottomAnchor. constraint ( equalTo: viewBottomAnchor)
87+ bottomConstraint? . isActive = true
6888 }
69-
89+
7090 @objc
7191 private func keyboardWillChangeFrame( _ note: Notification ) {
7292 if var height = note. keyboardHeight {
73- if #available( iOS 11 . 0 , * ) , height > 0 , let bottom = owningView? . safeAreaInsets. bottom {
93+ if #available( iOS 11 . 0 , * ) , usesSafeArea , height > 0 , let bottom = owningView? . safeAreaInsets. bottom {
7494 height -= bottom
7595 }
7696 heightConstraint? . constant = height
7797 animate ( note)
7898 Keyboard . shared. currentHeight = height
7999 }
80100 }
81-
101+
82102 private func animate( _ note: Notification ) {
83103 if
84104 let owningView = self . owningView,
0 commit comments