5.8 C
New York
Thursday, October 17, 2024

ios – UITextContent sort doesn’t all the time work in SwiftUI


I’ve an app that requires a number of kinds with textfields like login, forgot password, create account and so on.

My aim is to have the ability to present recommendations in these textfields utilizing SwiftUIs textContentType modifier.

I created a customized textfield which might be used as an everyday textfield or a safe textfield like for a password and apply some common styling.

For simplicity sake, here’s a simplified model the textfield to display the issue:

struct CustomTextField: View {
    @StateObject var viewModel: TextInputFieldViewModel
    
    var physique: some View {
        Group {
            if viewModel.isSecure {
                SecureField("", textual content: $viewModel.textual content)
                    .body(peak: 50)
                    .background(.grey)
            } else {
                TextField("", textual content: $viewModel.textual content)
                    .textContentType(viewModel.textContentType)
                    .body(peak: 50)
                    .background(.grey)
            }
        }
        
    }
}

The viewModel driving this view:

class TextInputFieldViewModel: ObservableObject {
    @Revealed var textual content: String
    @Revealed var isSecure: Bool
    
    let textContentType: UITextContentType
    
    init(textual content: String,
         isSecure: Bool = false,
         textContentType: UITextContentType) {
        self.textual content = textual content
        self.isSecure = isSecure
        self.textContentType = textContentType
    }
}

Now I exploit one textfield with the contentType set to e-mail, this works effectively:

struct ContentView: View {
    var physique: some View {
        VStack(spacing: 20) {
            CustomTextField(viewModel: TextInputFieldViewModel(textual content: "",
                                                               isSecure: false,
                                                               textContentType: .emailAddress))
        }
        .padding()
    }
}

UITextContent type SwiftUI email suggestions

Now if I attempt to construct a login title with an e-mail and password, the e-mail suggestion not works:

struct ContentView: View {
    var physique: some View {
        VStack(spacing: 20) {
            CustomTextField(viewModel: TextInputFieldViewModel(textual content: "",
                                                               isSecure: false,
                                                               textContentType: .emailAddress))
            
            CustomTextField(viewModel: TextInputFieldViewModel(textual content: "",
                                                               isSecure: true,
                                                               textContentType: .password))
        }
        .padding()
    }
}

As you see, no recommendations:

UITextContent type SwiftUI email suggestions TextField

If I construct a create account type like this:

struct ContentView: View {
    var physique: some View {
        VStack(spacing: 20) {
            CustomTextField(viewModel: TextInputFieldViewModel(textual content: "",
                                                               isSecure: false,
                                                               textContentType: .emailAddress))
            
            CustomTextField(viewModel: TextInputFieldViewModel(textual content: "",
                                                               isSecure: false,
                                                               textContentType: .givenName))
            
            CustomTextField(viewModel: TextInputFieldViewModel(textual content: "",
                                                               isSecure: false,
                                                               textContentType: .familyName))
            
            CustomTextField(viewModel: TextInputFieldViewModel(textual content: "",
                                                               isSecure: true,
                                                               textContentType: .password))
        }
        .padding()
    }
}

The primary two fields of e-mail and given title give recommendations, however the household title has no recommendations which occurs to be earlier than the safe discipline.

I believed perhaps my household title wasn’t arrange, however this wasn’t the case as if I swap the order round to e-mail, household title, given title and password – the given title would not present any recommendations.

It appears no matter is the sector earlier than the safe textual content enter would not present recommendations.

Within the CustomTextField, if I take away the if-else logic and simply maintain a TextField solely, this works advantageous, nevertheless, I would really like it to work with a reusable view that may work as each an everyday and safe textfield.

Any concepts / workarounds to this ?

I would like this to help iOS 16+.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles