UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

.autocorrectionDisabled() not updating with @State variable

Forums > SwiftUI

Using 'true' or 'false' works as expected:

TextEditor(text: $input)
    .autocorrectionDisabled(true)

But when replacing with a State variable, only the initial value is used. When the state value changes to 'false', the behavior of autocorrect doesn't change.

@State var shouldDisable = true  

TextEditor(text: $input)  
    .autocorrectionDisabled(shouldDisable)  
    .onReceive(aTimer) { \_ in  
        if otherConditionMet {  
            withAnimation {  
                shouldDisable = false  
            }  
        print(shouldDisable)  
        }  
    }

The timer is firing every second to evaluate if otherConditionMet. Here's the output of the simple print statement confirming shouldDisable updates when the other condition is met:

`shouldDisable: true`
`shouldDisable: true`
`shouldDisable: true`
`shouldDisable: true`
`shouldDisable: true
`shouldDisable: false`
`shouldDisable: false`
`shouldDisable: false`

I've validated by initialing 'shouldDisable' to true and again initializing to false and swapping shouldDisable = true in the if statement. In both conditions, only the initial value is used, despite how the variable changes.

Thanks for any input!

   

Update: I just validated this with the follwoing bare-bones file. The bool toggles to false, but the TextEditor does not re-enable autocorrection:

struct ContentView: View {
    @State var input = ""
    @State var autocorrectionDisabled = true

    var body: some View {

        VStack {
            TextEditor(text: $input)
                .autocorrectionDisabled(autocorrectionDisabled)
                .font(.headline)

            Button("Toggle Autocorrection") {
                autocorrectionDisabled.toggle()
                print("autocorrectionDisabled: \(autocorrectionDisabled)")
            }
            .padding()
        }
        .padding()
    }
}

I'd be interested to hear others' results and ideas about how to work around this.

   

When you're absolutely sure that you're passing in the correct value, you probably aren't.

Dashboard View

I like to create DashboardViews to show my @State variables and other bindings. Using red and green colored icons 🔴🟢 reminds me of a car's dashboard Check Engine light. So, like you, I created a simple version. Check it out....

struct AutoKorrectVeew: View {
    @State private var spell         = "Write the korrect spall here."
    @State private var spellingCharm = true
    var body: some View {
        VStack {
            // Add a dashboard view to test your assumptions about your state variables.
            DashboardView(isOn: spellingCharm).padding(.bottom)

            Text("Write a Spell!").font(.largeTitle)
            Toggle("Cast Charm", isOn: $spellingCharm).padding(.vertical) // <- Use a toggle!
            TextEditor(text: $spell)
                .keyboardType(.default)
                .autocorrectionDisabled(spellingCharm)
        }
        .padding()
    }
}

// Are you sure your variable is updating?
// Construct a simple dashboard to show the state of your variables.
struct DashboardView: View {
    var isOn: Bool
    var dashboardIcon: Image {
        // 🔴🟢
        isOn ? Image(systemName: "checkmark.seal.fill") : Image(systemName: "seal.fill")
    }
    var iconColor: Color {
        isOn ? .green : .red.opacity(0.4)
    }

    var body: some View {
        dashboardIcon.imageScale(.large)
            .foregroundColor(iconColor)
    }
}

Validation!

I have no solution for you! 😿
My results were the same as yours. Changing the autocorrrectionDisabled bool value seems to have no effect.

I thought it might have something to do with the keyboard type? But that too yielded nada.

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.