Skip to content

Commit 09ac33b

Browse files
committed
Add isActive callback for KeyboardManager.bind
1 parent 6677def commit 09ac33b

1 file changed

Lines changed: 43 additions & 28 deletions

File tree

Sources/KeyboardManager/KeyboardManager.swift

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,12 @@ open class KeyboardManager: NSObject, UIGestureRecognizerDelegate {
169169
///
170170
/// - Parameter inputAccessoryView: The view to bind to the top of the keyboard but within its superview
171171
/// - Returns: Self
172-
@discardableResult
173-
open func bind(inputAccessoryView: UIView, withAdditionalBottomSpace additionalBottomSpace: (() -> CGFloat)? = .none) -> Self {
172+
@discardableResult
173+
open func bind<InputAccessoryView: UIView>(
174+
inputAccessoryView: InputAccessoryView,
175+
isActive: @escaping (InputAccessoryView) -> Bool = { _ in true },
176+
withAdditionalBottomSpace additionalBottomSpace: (() -> CGFloat)? = .none
177+
) -> Self {
174178

175179
guard let superview = inputAccessoryView.superview else {
176180
fatalError("`inputAccessoryView` must have a superview")
@@ -186,62 +190,73 @@ open func bind(inputAccessoryView: UIView, withAdditionalBottomSpace additionalB
186190

187191
callbacks[.willShow] = { [weak self] (notification) in
188192
guard
189-
self?.isKeyboardHidden == false,
190-
self?.constraints?.bottom?.constant == self?.additionalInputViewBottomConstraintConstant(),
191-
notification.isForCurrentApp
193+
let self,
194+
isKeyboardHidden == false,
195+
constraints?.bottom?.constant == additionalInputViewBottomConstraintConstant(),
196+
notification.isForCurrentApp,
197+
let inputAccessoryView = self.inputAccessoryView as? InputAccessoryView
192198
else { return }
193199

194-
let keyboardHeight = notification.endFrame.height
200+
let keyboardHeight = isActive(inputAccessoryView) ? notification.endFrame.height : 0
195201
let animateAlongside = {
196-
self?.animateAlongside(notification) {
197-
self?.constraints?.bottom?.constant = min(0, -keyboardHeight + (self?.bottomGap ?? 0)) - (additionalBottomSpace?() ?? 0)
198-
self?.inputAccessoryView?.superview?.layoutIfNeeded()
202+
self.animateAlongside(notification) {
203+
self.constraints?.bottom?.constant = min(-34, -keyboardHeight + self.bottomGap) - (additionalBottomSpace?() ?? 0)
204+
self.inputAccessoryView?.superview?.layoutIfNeeded()
199205
}
200206
}
201207
animateAlongside()
202208

203209
// Trigger a new animation if gap changed, this typically happens when using pagesheet on portrait iPad
204-
let initialBottomGap = self?.bottomGap ?? 0
205-
DispatchQueue.main.async {
206-
let newBottomGap = self?.bottomGap ?? 0
210+
let initialBottomGap = bottomGap
211+
DispatchQueue.main.async { [weak self] in
212+
guard let self else { return }
213+
let newBottomGap = bottomGap
207214
if newBottomGap != 0 && newBottomGap != initialBottomGap {
208215
animateAlongside()
209216
}
210217
}
211218
}
212219
callbacks[.willChangeFrame] = { [weak self] (notification) in
213-
let keyboardHeight = notification.endFrame.height
214220
guard
215-
self?.isKeyboardHidden == false,
216-
notification.isForCurrentApp
221+
let self,
222+
isKeyboardHidden == false,
223+
notification.isForCurrentApp,
224+
let inputAccessoryView = self.inputAccessoryView as? InputAccessoryView
217225
else {
218226
return
219227
}
228+
let keyboardHeight = isActive(inputAccessoryView) ? notification.endFrame.height : 0
220229
let animateAlongside = {
221-
self?.animateAlongside(notification) {
222-
self?.constraints?.bottom?.constant = min(0, -keyboardHeight + (self?.bottomGap ?? 0)) - (additionalBottomSpace?() ?? 0)
223-
self?.inputAccessoryView?.superview?.layoutIfNeeded()
230+
self.animateAlongside(notification) {
231+
self.constraints?.bottom?.constant = min(-34, -keyboardHeight + self.bottomGap) - (additionalBottomSpace?() ?? 0)
232+
self.inputAccessoryView?.superview?.layoutIfNeeded()
224233
}
225234
}
226235
animateAlongside()
227236

228237
// Trigger a new animation if gap changed, this typically happens when using pagesheet on portrait iPad
229-
let initialBottomGap = self?.bottomGap ?? 0
230-
DispatchQueue.main.async {
231-
let newBottomGap = self?.bottomGap ?? 0
232-
if newBottomGap != 0 && newBottomGap != initialBottomGap && !(self?.justDidWillHide ?? false) {
238+
let initialBottomGap = bottomGap
239+
DispatchQueue.main.async { [weak self] in
240+
guard let self else { return }
241+
let newBottomGap = bottomGap
242+
if newBottomGap != 0 && newBottomGap != initialBottomGap && !(justDidWillHide) {
233243
animateAlongside()
234244
}
235245
}
236246
}
237247
callbacks[.willHide] = { [weak self] (notification) in
238-
guard notification.isForCurrentApp else { return }
239-
self?.justDidWillHide = true
240-
self?.animateAlongside(notification) { [weak self] in
241-
self?.constraints?.bottom?.constant = self?.additionalInputViewBottomConstraintConstant() ?? 0
242-
self?.inputAccessoryView?.superview?.layoutIfNeeded()
248+
guard
249+
let self,
250+
notification.isForCurrentApp
251+
else {
252+
return
253+
}
254+
justDidWillHide = true
255+
animateAlongside(notification) {
256+
self.constraints?.bottom?.constant = self.additionalInputViewBottomConstraintConstant()
257+
self.inputAccessoryView?.superview?.layoutIfNeeded()
243258
}
244-
DispatchQueue.main.async {
259+
DispatchQueue.main.async { [weak self] in
245260
self?.justDidWillHide = false
246261
}
247262
}

0 commit comments

Comments
 (0)