Skip to content

Commit

Permalink
Merge pull request nathantannar4#152 from AndrewSB/dismissal-animations
Browse files Browse the repository at this point in the history
InputTextView: allow setting of canBecomeFirstResponder + add example
  • Loading branch information
Kaspik authored Mar 4, 2021
2 parents d69d7de + 579835a commit adf69db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Example/Example/SubviewExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,22 @@ final class SubviewExampleViewController: CommonTableViewController {
// Binding to the tableView will enabled interactive dismissal
keyboardManager.bind(to: tableView)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

/// This replicates instagram's behavior when commenting in a post. As of 2020-09, it appears like they have one of the best product experiences of this handling the keyboard when dismissing the UIViewController
self.inputBar.inputTextView.resignFirstResponder()
/// This is set because otherwise, if only partially dragging the left edge of the screen, and then cancelling the dismissal, on viewDidAppear UIKit appears to set the first responder back to the inputTextView (https://stackoverflow.com/a/41847448)
self.inputBar.inputTextView.canBecomeFirstResponder = false
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

/// The opposite of `viewWillDisappear(_:)`
self.inputBar.inputTextView.canBecomeFirstResponder = true
self.inputBar.inputTextView.becomeFirstResponder()
}

}
6 changes: 6 additions & 0 deletions Sources/Views/InputTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ open class InputTextView: UITextView {

open var isImagePasteEnabled: Bool = true

private var canBecomeFirstResponderStorage: Bool = true
open override var canBecomeFirstResponder: Bool {
get { canBecomeFirstResponderStorage }
set(newValue) { canBecomeFirstResponderStorage = newValue }
}

/// A UILabel that holds the InputTextView's placeholder text
public let placeholderLabel: UILabel = {
let label = UILabel()
Expand Down

0 comments on commit adf69db

Please sign in to comment.