title: iOS How to make a text view which is in an inputaccessoryview a first responder?
body: <p>I want a textview in accessory view but the accessory view should be visible only when I press a button in a scroll view and the keyboard should close interactively with the text view on swipe. What is the solution?</p>
  
  <p>Facebook messenger &amp; WhatsApp does the same thing, but they have the accessory view visible even when the keyboard is closed, but I don't want it.</p>
  
  <p>My solution: I made the accessory view transparent, and the textview is a subview of scrollview and I'll position the textview right above the transparent accessory view, so it will look like accessory view. But when I do this, the textview is not touchable as the accessoryview window is above it.</p>
  
  <p>My code:</p>
  
  <pre><code>class ViewController: UIViewController, CustomKeyboardProtocol {
  
  let textView = UITextView()
  let button = UIButton()
  let scrollView = UIScrollView()
  
  override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
      super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      self.view.backgroundColor = .black
  
      scrollView.contentSize = CGSize(width: 10000, height: 10000)
      scrollView.keyboardDismissMode = .interactive
      scrollView.backgroundColor = .blue
      self.view.addSubview(scrollView)
  
      button.backgroundColor = .green
      button.frame = CGRect(x: 20, y: 20, width: 30, height: 30)
      button.addTarget(self, action: #selector(keyboard), for: .touchUpInside)
      scrollView.addSubview(button)
  
      textView.backgroundColor = .yellow
      textView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40)
      self.view.addSubview(textView)
      textView.isHidden = true
  
      let accessoryView = CustomKeyboardAccessoryView(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
      accessoryView.keyboardDelegate = self
      accessoryView.backgroundColor = .clear
      textView.inputAccessoryView = accessoryView
  }
  
  func keyboardFrameChanged(frame: CGRect) {
      textView.frame = CGRect(x: 0, y: frame.origin.y, width: UIScreen.main.bounds.size.width, height: 40)
  }
  
  @objc func keyboardWillShow(notification: Notification) {
      if let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
          let keyboardHeight = keyboardFrame.size.height
          print("keyboard height: \(keyboardHeight)")
          textView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - keyboardHeight, width: UIScreen.main.bounds.size.width, height: 40)
      }
  }
  
  @objc func keyboardWillHide() {
      textView.isHidden = true
      textView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0)
  }
  
  @objc func keyboard() {
      if textView.isFirstResponder {
          textView.resignFirstResponder()
          textView.isHidden = true
      } else {
          textView.becomeFirstResponder()
          textView.isHidden = false
      }
  }
  
  override func viewWillLayoutSubviews() {
      super.viewWillLayoutSubviews()
      scrollView.frame = UIScreen.main.bounds
  }
  
  override var canBecomeFirstResponder: Bool {
      return true
  }
  }
  
  protocol CustomKeyboardProtocol : NSObjectProtocol {
      func keyboardFrameChanged(frame : CGRect)
  }
  
  class CustomKeyboardAccessoryView: UIView {
  
  
  
  weak var keyboardDelegate: CustomKeyboardProtocol? = nil
  
  override func willMove(toSuperview newSuperview: UIView?) {
      if newSuperview == nil {
          self.superview?.removeObserver(self, forKeyPath: "center")
      }
      else{
          newSuperview?.addObserver(self, forKeyPath: "center", options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.initial], context: nil)
      }
  }
  
  override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
      if let theChange = change as [NSKeyValueChangeKey : AnyObject]? {
          if theChange[NSKeyValueChangeKey.newKey] != nil {
              if self.keyboardDelegate != nil &amp;&amp; self.superview?.frame != nil {
                  self.keyboardDelegate?.keyboardFrameChanged(frame: (self.superview?.frame)!)
              }
          }
      }
  
  
     }
  }
  </code></pre>