Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FB13727682: SwiftUI scrollable TextFields not rendering correctly in certain autocomplete cases #526

Open
AnthonyWharton opened this issue Apr 14, 2024 · 1 comment

Comments

@AnthonyWharton
Copy link

  • Date: 2024-04-14
  • Resolution: Open
  • Area: SwiftUI
  • OS: iOS
  • Type: Incorrect/Unexpected Behaviour

Description

When using a multi-line scrollable TextField, there are some cases where clearing the text state variable will not cause the TextField to re-draw itself. In particular, in cases where the autocompletion has provided a suggestion in “greyed out” text.

For example when I type “Fair enough”, the when I get to “Fair en” the system will autofill in light grey text “ough”. This changes the underlying text variable bound to the TextField to the fully completed text.

In cases where the user continues to manually type the text to completion (i.e. does not press on the word suggestion bar, or does not move to another focus which implicitly keeps the suggestion), and then presses a button that clears the TextField, the text state variable will reset, however the TextField will not redraw to be empty again. We would expect the TextField to return to an empty state, with only the greyed out prompt showing.

Please find attached:

  • A minimum reproducible example case.
  • A short 16 second video showing the unexpected behaviour in the provided example code (when typing to completion), followed by an example of expected behaviour (when pressing the autocomplete suggestion in the keyboard).

Tested on iOS 17.4.1, and Xcode 15.2.

Files

TextFieldBug.zip
TextFieldBug.mov

@AnthonyWharton
Copy link
Author

AnthonyWharton commented Apr 14, 2024

If anyone comes across this issue in the meantime, I have found the following workaround using SwiftUIIntrospect

import SwiftUIIntrospect

// ...

TextField("...", text: $text, axis: .vertical)
    // modifiers...
    .introspect(.textField(axis: .vertical), on: .iOS(.v17)) {
        // $0 is a UITextView used behind the scenes by SwiftUI
        $0.inlinePredictionType = .no
    }

Further reading:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant