title: Avoiding Keyboard in SwifUI with TextEditor
body: <p>I'm attempting to recreate a simple version of the iOS notes app. Mind you, I'm a complete Swift novice. My current issue is that I want my view to move up as the keyboard appears. I've implemented some code that <em>does</em> do this, but it has some nasty bugs. It first moves the view up WAY too high, then when you begin typing, the view is where it should be. Here are some photos for representation, as well as my code:</p>
  <p><a href="https://i.stack.imgur.com/uUJm5.png" rel="nofollow noreferrer">Before the keyboard appears</a></p>
  <p><a href="https://i.stack.imgur.com/8qtvo.png" rel="nofollow noreferrer">When the keyboard first appears</a></p>
  <p><a href="https://i.stack.imgur.com/T14UU.png" rel="nofollow noreferrer">Once you begin typing</a></p>
  <p>Code:</p>
  <pre><code>    class KeyboardResponder: ObservableObject {
  
      @Published var currentHeight: CGFloat = 0
  
      var _center: NotificationCenter
      
      @objc func keyBoardWillShow(notification: Notification) {
      if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
                  withAnimation {
                     currentHeight = keyboardSize.height
                      print(&quot;KEYBOARDSIZE.HEIGHT IN OBSERVER: \(keyboardSize.height)&quot;)
                  }
              }
          print(&quot;KEYBOARD HEIGHT IN OBSERVER: \(currentHeight)&quot;)
          }
      @objc func keyBoardWillHide(notification: Notification) {
              withAnimation {
                 currentHeight = 0
              }
          }
  
          init(center: NotificationCenter = .default) {
              _center = center
              _center.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
              _center.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
          }
  }
  </code></pre>
  <p>First, there is a KeyboardResponder class, listening for keyboard appearances and disappearances (with a published variable for its height).</p>
  <pre><code>        VStack {
          
         TextEditor(text: $content).padding(.all).foregroundColor(fontColors[self.color]).font(fontStyles[self.font])
  
          HStack {
              Spacer()
              Button(action: {
                  self.show = false
              }) {
                  Text(&quot;Cancel&quot;).foregroundColor(.gray).font(.headline)
              }
              
              Spacer()
              
              Button(action: {
                  self.showPanel = true
              }) {
                  Image(systemName: &quot;textformat&quot;).font(.headline).foregroundColor(.white).padding(.all)
              }.background(Color.green).clipShape(Circle())
              
              Spacer()
              
              Button(action: {
                  self.show.toggle()
                  self.saveData()
              }) {
                  Text(&quot;Save&quot;).foregroundColor(Color(UIColor.systemBlue)).font(.headline)
              }
              Spacer()
          }
          
      }.padding(.bottom, keyboardResponder.currentHeight)
  </code></pre>
  <p>This is the view, with the editor shown in the photos. At the top of this view I have @ObservedObject var keyboardResponder = KeyboardResponder(). I've tried both .padding(.bottom, keyboardResponder.currentHeight) as well as .offset(y: -keyboardResponder.currentHeight). Anyone know what's going on?</p>