title: Creating an Input Accessory View programmatically
body: <p>I'm trying to create a keyboard accessory view programmatically. I've set up a container view and inside that I'm trying to set up a textfield, post button, and an emoji. 
  Here's an example of what I'm trying to make.  </p>
  
  <p><a href="https://i.stack.imgur.com/d50lg.png" rel="nofollow noreferrer">Click here to view the image.</a></p>
  
  <p>Here's the code that I am working with. I think the problem is when I'm setting the constraints. 
  Couple of questions running through my mind are: </p>
  
  <ol>
  <li><p>Do I need to set up constraints for the container view? </p></li>
  <li><p>How do I add appropriate constraints to the textfield? </p>
  
  <pre><code>override var inputAccessoryView: UIView? {
  get {
      //Set up the container
      let containerView = UIView()
      containerView.backgroundColor = #colorLiteral(red: 0.9784782529, green: 0.9650371671, blue: 0.9372026324, alpha: 1)
      containerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 60)
  
      let textField = UITextField()
      textField.placeholder = "Add a reframe..."
      textField.isSecureTextEntry = false
      textField.textAlignment = .left
      textField.borderStyle = .none
      textField.translatesAutoresizingMaskIntoConstraints = false
  
  
      textField.translatesAutoresizingMaskIntoConstraints = false
      textField.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
      textField.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
      textField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
      textField.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
      textField.heightAnchor.constraint(equalToConstant: 50)
  
  
      containerView.addSubview(textField)
  
      return containerView
  }
  }
  </code></pre></li>
  </ol>
  
  <p>This is the error that I keep getting.</p>
  
  <p>*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors  and  because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>View Post Controller</p>
  
  <pre><code>lazy var containerView: CommentInputAccessoryView = {
      let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 60)
      let commentInputAccessoryView = CommentInputAccessoryView(frame: frame)
      commentInputAccessoryView.delegate = self
      return commentInputAccessoryView
  }()
  
  //Setting up the keyboard accessory view for comments.
  override var inputAccessoryView: UIView? {
      get {
          return containerView
      }
  }
  override var canBecomeFirstResponder: Bool {
      return true
  }
  </code></pre>
  
  <p>CommentInputAccessoryView </p>
  
  <pre><code>protocol CommentInputAccessoryViewDelegate {
  func didSend(for comment: String)
  }
  
  class CommentInputAccessoryView: UIView {
  
  var delegate: CommentInputAccessoryViewDelegate?
  
  func clearCommentTextView() {
      commentTextView.text = nil
      showPlaceholderLabel()
  }
  
  let commentTextView: UITextView = {
      let text = UITextView()
      text.translatesAutoresizingMaskIntoConstraints = false
      //text.placeholder = "Add a reframe..."
      text.textAlignment = .left
      text.backgroundColor = #colorLiteral(red: 0.9784782529, green: 0.9650371671, blue: 0.9372026324, alpha: 1)
      text.layer.cornerRadius = 50/2
      text.layer.masksToBounds = true
      text.isScrollEnabled = false
      text.font = UIFont.systemFont(ofSize: 16)
      text.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 64)
      //text.borderStyle = .none
      return text
  }()
  
  let placeholderLabel: UILabel = {
      let label = UILabel()
      label.translatesAutoresizingMaskIntoConstraints = false
      label.text = "Add a response..."
      label.textColor = .black
      label.font = UIFont.systemFont(ofSize: 16)
      return label
  }()
  
  func showPlaceholderLabel() {
      placeholderLabel.isHidden = false
  }
  
  let sendButton: UIButton = {
      let send = UIButton(type: .system)
      //let sendButton = UIImageView(image: #imageLiteral(resourceName: "arrowUp"))
      send.translatesAutoresizingMaskIntoConstraints = false
      send.setTitle("Send", for: .normal)
      send.setTitleColor(#colorLiteral(red: 0.2901960784, green: 0.3725490196, blue: 0.937254902, alpha: 1), for: .normal)
      send.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
      send.addTarget(self, action: #selector(handlePostComment), for: .touchUpInside)
      return send
  }()
  
  let hugButton: UIButton = {
      let hug = UIButton()
      hug.translatesAutoresizingMaskIntoConstraints = false
      hug.setTitle("", for: .normal)
      hug.backgroundColor = #colorLiteral(red: 0.9784782529, green: 0.9650371671, blue: 0.9372026324, alpha: 1)
      hug.contentEdgeInsets = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
      hug.layer.cornerRadius = 25
      hug.layer.masksToBounds = true
      return hug
  }()
  
  
  override init(frame: CGRect) {
      super.init(frame: frame)
      autoresizingMask = .flexibleHeight
  
      addSubview(commentTextView)
      addSubview(sendButton)
      addSubview(hugButton)
      addSubview(placeholderLabel)
  
  
      if #available(iOS 11.0, *) {
          commentTextView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
          hugButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      }
      else {
      }
  
      commentTextView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 8).isActive = true
      commentTextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
      commentTextView.trailingAnchor.constraint(equalTo: hugButton.leadingAnchor, constant: 8)
  
      placeholderLabel.leadingAnchor.constraint(equalTo: commentTextView.leadingAnchor, constant: 18).isActive = true
      placeholderLabel.centerYAnchor.constraint(equalTo: self.commentTextView.centerYAnchor).isActive = true
  
      sendButton.trailingAnchor.constraint(equalTo: self.commentTextView.trailingAnchor, constant: -10).isActive = true
      sendButton.centerYAnchor.constraint(equalTo: self.commentTextView.bottomAnchor, constant: -22).isActive = true
  
      hugButton.leadingAnchor.constraint(equalTo: self.commentTextView.trailingAnchor, constant: 10).isActive = true
      hugButton.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8).isActive = true
      hugButton.widthAnchor.constraint(equalToConstant: 40)
      //hugButton.centerYAnchor.constraint(equalTo: self.commentTextView.centerYAnchor).isActive = true
  
      NotificationCenter.default.addObserver(self, selector: #selector(handleTextChanged), name: .UITextViewTextDidChange, object: nil)
  }
  
  override var intrinsicContentSize: CGSize {
      return .zero
  }
  
  @objc func handleTextChanged() {
      placeholderLabel.isHidden = !self.commentTextView.text.isEmpty
  }
  
  @objc func handlePostComment() {
      guard let commentText = commentTextView.text else {return}
      delegate?.didSend(for: commentText)
  }
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  }
  </code></pre>
  
  <p>Here are some photos that might help for what is happening. </p>
  
  <p>InputAccessoryView working: 
  <a href="https://i.stack.imgur.com/mBeE4.png" rel="nofollow noreferrer">Click here</a></p>
  
  <p>TextView expansion:
  <a href="https://i.stack.imgur.com/OnHVl.png" rel="nofollow noreferrer">Click here.</a></p>