title: SwiftUI inputAccesoryView Implementation
body: <p>I am trying to implement an inputAccessoryView on a TextField in SwiftUI. The goal is to have a "Done" Button appear above the Keyboard which when pressed gets rid of the keyboard (i.e. resignFirstResponder()).</p>
  
  <p>I came across the following Medium article which purports to implement this behavior exactly as I would require, however, I am struggling to get it working.</p>
  
  <p><a href="https://medium.com/better-programming/using-uireturnkeytype-or-inputaccessoryview-in-swiftui-c140a24cd112" rel="nofollow noreferrer">Medium link containing method to be implemented.</a></p>
  
  <p>I have tried to implement this in a blank XCode project, my code compiles, however, the TextField never shows up, and I cannot touch in the area it should be to bring up the keyboard. How do I correctly implement this code to get the desired behavior?</p>
  
  <h1>Code</h1>
  
  <pre><code>    import Foundation
  import UIKit
  import SwiftUI
  
  class TextFieldViewController
      : UIViewController {
  
      // our custom text field will report changes to the outside
      let text: Binding&lt;String&gt;?
  
      // if the toolbar (see below) is used (Done), the keyboard shall be dismissed
      // and optionally we execute a provided closure
      let onDismiss: (() -&gt; Void)?
  
      init (
          text: Binding&lt;String&gt;
          , onDismiss: (() -&gt; Void)?) {
  
          self.text = text
          self.onDismiss = onDismiss
  
          super.init(
              nibName: nil //"&lt;XIB&gt;"
              , bundle: nil //Bundle.main?
          )
      }
  
      required init?(coder: NSCoder) {
          self.text = nil
          self.onDismiss = nil
  
          super.init(coder: coder)
      }
  
      // helper function to encapsulate calling the "view" of UIViewController
      fileprivate func getTextField() -&gt; UITextField? {
          return view as? UITextField
      }
  
      override func viewDidLoad() {
          let textField = self.getTextField()
          guard textField != nil else {
              return
          }
  
          // configure a toolbar with a Done button
          let toolbar = UIToolbar()
          toolbar.setItems([
                  // just moves the Done item to the right
                  UIBarButtonItem(
                      barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace
                      , target: nil
                      , action: nil
                  )
                  , UIBarButtonItem(
                      title: "Done"
                      , style: UIBarButtonItem.Style.done
                      , target: self
                      , action: #selector(self.onSet)
                  )
              ]
              , animated: true
          )
          toolbar.barStyle = UIBarStyle.default
          toolbar.sizeToFit()
          textField?.inputAccessoryView = toolbar
      }
  
      @objc private func onSet() {
          let textField = self.getTextField()
          textField?.resignFirstResponder()
  
          self.text?.wrappedValue = textField?.text ?? ""
          self.onDismiss?()
      }
  
  }
  
  // The SwiftUI view, wrapping the UITextField
  struct TextFieldView: View {
  
      var text: Binding&lt;String&gt;
      var onDismissKeyboard: (() -&gt; Void)?
  
      var body: some View {
          TextFieldRepresentable(
              text: self.text
              , dismissKeyboardCallback: self.onDismissKeyboard
          )
      }
  }
  
  // The UIViewControllerRepresentable, feeding and controlling the UIViewController
  struct TextFieldRepresentable
      : UIViewControllerRepresentable {
  
      // the callback
      let dismissKeyboardCallback: (() -&gt; Void)?
  
      // created in the previous file/gist
      let viewController: TextFieldViewController
  
      init (
          text: Binding&lt;String&gt;
          , dismissKeyboardCallback: (() -&gt; Void)?) {
  
          self.dismissKeyboardCallback = dismissKeyboardCallback
          self.viewController = TextFieldViewController(
              text: text
              , onDismiss: dismissKeyboardCallback
          )
      }
  
      // UIViewControllerRepresentable
      func makeUIViewController(context: Context) -&gt; UIViewController {
  
          return viewController
      }
  
      // UIViewControllerRepresentable
      func updateUIViewController(_ viewController: UIViewController, context: Context) {
      }
  
  }
  
  struct ContentView : View {
  
      @State var email:String = ""
  
      var body: some View {
  
          HStack{
          Circle()
          TextFieldView(text: $email)
          Circle()
          }
      }
  } 
  </code></pre>