Skip to content

Commit

Permalink
InputTextView: allow settig of canBecomeFirstResponder + add example
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSB committed Sep 17, 2020
1 parent 5a9c944 commit 579835a
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 @@ -39,5 +39,22 @@ final class SubviewExampleViewController: CommonTableViewController {
self?.tableView.scrollIndicatorInsets.bottom = barHeight
}
}

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 579835a

Please sign in to comment.