InputBarViewController.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // InputBarViewController.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 9/13/18.
  26. //
  27. import UIKit
  28. /// An simple `UIViewController` subclass that is ready to work
  29. /// with an `inputAccessoryView`
  30. open class InputBarViewController: UIViewController, InputBarAccessoryViewDelegate {
  31. /// A powerful InputAccessoryView ideal for messaging applications
  32. public let inputBar = InputBarAccessoryView()
  33. /// A boolean value that when changed will update the `inputAccessoryView`
  34. /// of the `InputBarViewController`. When set to `TRUE`, the
  35. /// `inputAccessoryView` is set to `nil` and the `inputBar` slides off
  36. /// the screen.
  37. ///
  38. /// The default value is FALSE
  39. open var isInputBarHidden: Bool = false {
  40. didSet {
  41. isInputBarHiddenDidChange()
  42. }
  43. }
  44. open override var inputAccessoryView: UIView? {
  45. return isInputBarHidden ? nil : inputBar
  46. }
  47. open override var canBecomeFirstResponder: Bool {
  48. return !isInputBarHidden
  49. }
  50. open override func viewDidLoad() {
  51. super.viewDidLoad()
  52. inputBar.delegate = self
  53. }
  54. /// Invoked when `isInputBarHidden` changes to become or
  55. /// resign first responder
  56. open func isInputBarHiddenDidChange() {
  57. if isInputBarHidden, isFirstResponder {
  58. resignFirstResponder()
  59. } else if !isFirstResponder {
  60. becomeFirstResponder()
  61. }
  62. }
  63. @discardableResult
  64. open override func resignFirstResponder() -> Bool {
  65. inputBar.inputTextView.resignFirstResponder()
  66. return super.resignFirstResponder()
  67. }
  68. // MARK: - InputBarAccessoryViewDelegate
  69. open func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) { }
  70. open func inputBar(_ inputBar: InputBarAccessoryView, textViewTextDidChangeTo text: String) { }
  71. open func inputBar(_ inputBar: InputBarAccessoryView, didChangeIntrinsicContentTo size: CGSize) { }
  72. open func inputBar(_ inputBar: InputBarAccessoryView, didSwipeTextViewWith gesture: UISwipeGestureRecognizer) { }
  73. }