Skip to content

Commit f1ff5da

Browse files
authored
Merge pull request #29 from abekert/feature/no-safe-area
Provide better API for using Safe Area.
2 parents 5172093 + 9f30f86 commit f1ff5da

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

KeyboardLayoutGuide/KeyboardLayoutGuide/Keyboard+LayoutGuide.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,32 @@ internal class Keyboard {
1414
}
1515

1616
extension UIView {
17-
private enum AssociatedKeys {
18-
static var keyboardLayoutGuide = "keyboardLayoutGuide"
17+
private enum Identifiers {
18+
static var usingSafeArea = "KeyboardLayoutGuideUsingSafeArea"
19+
static var notUsingSafeArea = "KeyboardLayoutGuide"
1920
}
2021

2122
/// A layout guide representing the inset for the keyboard.
22-
/// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard.
23-
public var keyboardLayoutGuide: KeyboardLayoutGuide {
24-
if let obj = objc_getAssociatedObject(self, &AssociatedKeys.keyboardLayoutGuide) as? KeyboardLayoutGuide {
25-
return obj
23+
/// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard or the bottom of safe area.
24+
public var keyboardLayoutGuide: UILayoutGuide {
25+
getOrCreateKeyboardLayoutGuide(identifier: Identifiers.usingSafeArea, usesSafeArea: true)
26+
}
27+
28+
/// A layout guide representing the inset for the keyboard.
29+
/// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard or the bottom of the view.
30+
public var keyboardLayoutGuideNoSafeArea: UILayoutGuide {
31+
getOrCreateKeyboardLayoutGuide(identifier: Identifiers.notUsingSafeArea, usesSafeArea: false)
32+
}
33+
34+
private func getOrCreateKeyboardLayoutGuide(identifier: String, usesSafeArea: Bool) -> UILayoutGuide {
35+
if let existing = layoutGuides.first(where: { $0.identifier == identifier }) {
36+
return existing
2637
}
2738
let new = KeyboardLayoutGuide()
39+
new.usesSafeArea = usesSafeArea
40+
new.identifier = identifier
2841
addLayoutGuide(new)
2942
new.setUp()
30-
objc_setAssociatedObject(self, &AssociatedKeys.keyboardLayoutGuide, new as Any, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
3143
return new
3244
}
3345
}

0 commit comments

Comments
 (0)