title: Changing the color of a button in SwiftUI based on disabled or not
body: <p>I have a textfield with a send button that's a systemImage arrow. I want the foreground color of the image to change depending on whether the textField is empty or not. (I.e. the button is gray, and it is disabled if the textfield is empty. It's blue if the count of the textfield text is &gt; 1).</p>
  <p>I have a workaround that's not perfect:</p>
  <pre class="lang-swift prettyprint-override"><code>
  if chatMessageIsValid {
      Spacer()
      HStack {
          TextField($chatMessage, placeholder: Text(&quot;Reply&quot;))
              .padding(.leading, 10)
              .textFieldStyle(.roundedBorder)
          Button(action: sendMessage) {
              Image(systemName: &quot;arrow.up.circle&quot;)
                  .foregroundColor(Color.blue)
                  .padding(.trailing, 10)
          }.disabled(!chatMessageIsValid)
      }
  } else {
      Spacer()
      HStack {
          TextField($chatMessage, placeholder: Text(&quot;Reply&quot;))
              .padding(.leading, 10)
              .textFieldStyle(.roundedBorder)
          Button(action: sendMessage) {
              Image(systemName: &quot;arrow.up.circle&quot;)
                  .foregroundColor(Color.gray)
                  .padding(.trailing, 10)
          }.disabled(!chatMessageIsValid)
      }
  }
  </code></pre>
  <p>This almost works, and it does change the color of the image if the text is &gt; 1 in length. However, due to the change in state you're kicked out of editing the textfield after one character is typed, and you'll need to select the textfield again to continue typing. Is there a better way to do this with the .disabled modifier?</p>