Skip to content

Best way to handle isSecureTextEntry #187

Closed Answered by SplittyDev
moyoteg asked this question in Q&A
Discussion options

You must be logged in to vote

It seems that SwiftUI is doing something weird behind the scenes.

I don't really have a fix for you here, but as a workaround I suggest using a binding inside of SecureToggle and rendering a SecureField instead of a normal TextField when isSecure is true.

Here's some example code on how the desired behavior might be achieved:

struct SecureToggle: ViewModifier {
    @Binding var isSecure: Bool
    public func body(content: Content) -> some View {
        HStack {
            content
            Spacer()
            Button(action: { self.isSecure.toggle() }) {
                Image(systemName: isSecure ? "eye.slash" : "eye")
                    .foregroundColor(Color.blue)
            }.pad…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by davdroman
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #72 on February 12, 2023 00:47.