Skip to content

Commit ed73d33

Browse files
authored
Merge pull request #27 from freshOS/safeArea
Adds usesSafeArea to opt out of safe Area behaviour
2 parents 29d11cd + 671d7a8 commit ed73d33

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

KeyboardLayoutGuide/KeyboardLayoutGuide/Keyboard+LayoutGuide.swift

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3535
open 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,

KeyboardLayoutGuideExample/KeyboardLayoutGuideExample/ViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class ViewController: UIViewController {
1616
override func viewDidLoad() {
1717
super.viewDidLoad()
1818

19+
// Opt out of safe area if needed.
20+
view.keyboardLayoutGuide.usesSafeArea = false
21+
1922
// Constrain your button to the keyboardLayoutGuide's top Anchor the way you would do natively :)
2023
button.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor).isActive = true
2124
}

0 commit comments

Comments
 (0)