Thursday, March 14, 2024
HomeiOS Developmentios - SwiftUI .autocorrectionDisabled() conduct not updating with @State BOOL

ios – SwiftUI .autocorrectionDisabled() conduct not updating with @State BOOL


Utilizing ‘true’ or ‘false’ works as anticipated:

TextEditor(textual content: $enter)
    .autocorrectionDisabled(true)

However when changing with a State variable, solely the preliminary worth is used. When the state worth modifications to ‘false’, the conduct of autocorrect would not change.

@State var shouldDisable = true  

TextEditor(textual content: $enter)  
    .autocorrectionDisabled(shouldDisable)  
    .onReceive(aTimer) { _ in  
        if otherConditionMet {  
            withAnimation {  
                shouldDisable = false  
            }  
        print(shouldDisable)  
        }  
    }

The timer is firing each second to guage if otherConditionMet. Here is the output of the easy print assertion confirming shouldDisable updates when the opposite situation 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 once more initializing to false and updating shouldDisable = true within the if assertion. In each circumstances, solely the preliminary worth is used, regardless of how the variable modifications.

I’ve tried eradicating the withAnimation, in addition to the order of the .autocorrectionDisabled modifier within the stack; neither had any impact.

I am testing on a bodily machine with iOS 15 as that is my oldest supported goal.

Thanks for any enter!


Replace: I simply validated this conduct with the next bare-bones code. The bool toggles to false, however the TextEditor doesn’t re-enable autocorrection:

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

    var physique: some View {

        VStack {
            TextEditor(textual content: $enter)
                .autocorrectionDisabled(autocorrectionDisabled)
                .font(.headline)
         
            Button("Toggle Autocorrection") {
                autocorrectionDisabled.toggle()
                print("autocorrectionDisabled: (autocorrectionDisabled)")
            }
            .padding()
        }
        .padding()
    }
}

I might have an interest to listen to others’ outcomes and concepts about tips on how to work round this.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments