Bird
0
0

How can you add a "Show Password" toggle to reveal or hide the password text in a SwiftUI form using SecureField?

hard📝 Application Q9 of 15
iOS Swift - User Input and Forms
How can you add a "Show Password" toggle to reveal or hide the password text in a SwiftUI form using SecureField?
AAdd a .showPassword(true) modifier to SecureField.
BUse a @State Bool to switch between SecureField and TextField based on toggle state.
CSet SecureField's text binding to optional String.
DUse only TextField and rely on system to mask input.
Step-by-Step Solution
Solution:
  1. Step 1: Understand SecureField limitations

    SecureField cannot toggle visibility by itself.
  2. Step 2: Implement toggle logic

    Use a @State Bool to conditionally show SecureField (masked) or TextField (visible) for password.
  3. Final Answer:

    Use a @State Bool to switch between SecureField and TextField based on toggle state. -> Option B
  4. Quick Check:

    Toggle visibility by switching fields [OK]
Quick Trick: Toggle between SecureField and TextField to show/hide password [OK]
Common Mistakes:
  • Expecting SecureField to have show/hide modifier
  • Using optional binding incorrectly
  • Relying on system masking only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes