Skip to content

Commit

Permalink
Merge pull request #27 from freshOS/safeArea
Browse files Browse the repository at this point in the history
Adds usesSafeArea to opt out of safe Area behaviour
  • Loading branch information
s4cha authored Jul 11, 2019
2 parents 29d11cd + 671d7a8 commit ed73d33
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
36 changes: 28 additions & 8 deletions KeyboardLayoutGuide/KeyboardLayoutGuide/Keyboard+LayoutGuide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension UIView {
private enum AssociatedKeys {
static var keyboardLayoutGuide = "keyboardLayoutGuide"
}

/// A layout guide representing the inset for the keyboard.
/// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard.
public var keyboardLayoutGuide: KeyboardLayoutGuide {
Expand All @@ -33,11 +33,19 @@ extension UIView {
}

open class KeyboardLayoutGuide: UILayoutGuide {
public var usesSafeArea = true {
didSet {
updateButtomAnchor()
}
}

private var bottomConstraint: NSLayoutConstraint?

@available(*, unavailable)
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public init(notificationCenter: NotificationCenter = NotificationCenter.default) {
super.init()
// Observe keyboardWillChangeFrame notifications
Expand All @@ -48,7 +56,7 @@ open class KeyboardLayoutGuide: UILayoutGuide {
object: nil
)
}

internal func setUp() {
guard let view = owningView else { return }
NSLayoutConstraint.activate(
Expand All @@ -58,27 +66,39 @@ open class KeyboardLayoutGuide: UILayoutGuide {
rightAnchor.constraint(equalTo: view.rightAnchor),
]
)
updateButtomAnchor()
}

func updateButtomAnchor() {
if let bottomConstraint = bottomConstraint {
bottomConstraint.isActive = false
}

guard let view = owningView else { return }

let viewBottomAnchor: NSLayoutYAxisAnchor
if #available(iOS 11.0, *) {
if #available(iOS 11.0, *), usesSafeArea {
viewBottomAnchor = view.safeAreaLayoutGuide.bottomAnchor
} else {
viewBottomAnchor = view.bottomAnchor
}
bottomAnchor.constraint(equalTo: viewBottomAnchor).isActive = true

bottomConstraint = bottomAnchor.constraint(equalTo: viewBottomAnchor)
bottomConstraint?.isActive = true
}

@objc
private func keyboardWillChangeFrame(_ note: Notification) {
if var height = note.keyboardHeight {
if #available(iOS 11.0, *), height > 0, let bottom = owningView?.safeAreaInsets.bottom {
if #available(iOS 11.0, *), usesSafeArea, height > 0, let bottom = owningView?.safeAreaInsets.bottom {
height -= bottom
}
heightConstraint?.constant = height
animate(note)
Keyboard.shared.currentHeight = height
}
}

private func animate(_ note: Notification) {
if
let owningView = self.owningView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// Opt out of safe area if needed.
view.keyboardLayoutGuide.usesSafeArea = false

// Constrain your button to the keyboardLayoutGuide's top Anchor the way you would do natively :)
button.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor).isActive = true
}
Expand Down

0 comments on commit ed73d33

Please sign in to comment.