I have a simple app in SwiftUI with a TextEditor and a button that brings up a SheetView with a toggle I named: disableAutoCorrection
When I toggle it, this should disable the auto corrector in my TextEditor, however, this only happens after fully closing and reopening the app.
Here's the code for the TextEditor:
TextEditor(text: $editorText)
.disableAutocorrection(userSettings.disableAutoCorrect ? true : false)
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0,
maxHeight: .infinity, alignment: .top)
.edgesIgnoringSafeArea(.bottom)
My Settings class:
class UserSettings: ObservableObject {
@Published var disableAutoCorrect: Bool {
didSet {
UserDefaults.standard.set(disableAutoCorrect, forKey: "disableAutoCorrect")
}
}
init() {
self.disableAutoCorrect = UserDefaults.standard.object(forKey: "disableAutoCorrect") as? Bool ?? false
}
}
In the TextEditor view I got a
@ObservedObject var userSettings = UserSettings()
SheetView with the toggle:
struct SettingsView: View {
@ObservedObject var userSettings = UserSettings()
var body: some View {
NavigationView {
Form {
Section(header: Text("Editor")) {
Toggle(isOn: $userSettings.disableAutoCorrect) {
Text("Disable AutoCorrection")
}
}
}
.navigationTitle("Settings")
}
}
}
class disabledAutoCorrector: ObservableObject {
@Published var disabledAuto: Bool = false
}
Thanks in advance!