Skip to content

Commit

Permalink
bugfix: space between keyboard and textfield.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhenry committed Jan 29, 2020
1 parent e5e13aa commit e1c3860
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Sources/KeyboardAvoider/KeyboardHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ class KeyboardHandler:NSObject, ObservableObject, UIGestureRecognizerDelegate {
}
.store(in: &subscriptions)

keyboardWillShow
let keyboardDidShow = NotificationCenter
.default
.publisher(for: UIResponder.keyboardDidShowNotification)

keyboardDidShow
.sink {[weak self] _ in
self?.adjustScrollViewOffsetYIfPossible()
}
Expand Down Expand Up @@ -126,14 +130,12 @@ class KeyboardHandler:NSObject, ObservableObject, UIGestureRecognizerDelegate {
let targetFrame = _activeView.convert(_activeView.bounds, to: nil)
let targetY = targetFrame.maxY
let containerY = UIScreen.main.bounds.height - keyboardHeight

if containerY < targetY {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
_scrollview.setContentOffset(
CGPoint(
x: 0,
y: _scrollview.contentOffset.y
+ CGFloat(self.spaceBetweenKeyboardAndInputField)),
animated: true)
DispatchQueue.main.async {
var newFrame = targetFrame
newFrame.origin.y -= self.spaceBetweenKeyboardAndInputField
_scrollview.scrollRectToVisible(newFrame, animated: true)
}
}
}
Expand Down

0 comments on commit e1c3860

Please sign in to comment.