11

I have a SecureField in SwiftUI. When this Field gets focused (when the user taps it) I want to manipulate a @State property for applying a offset modifier for the SecureField. When the SecureField gets out of focus the @State property should get reseted again. I want to do this for the SecureField to be visible if the keyboard gets toggled.

I achieved this with a TextField by using the onEditingChanged parameter of it, but this parameter doesn't exist for a SecureField.

How can I do this with a SecureField in SwiftUI?

This applies a offset if the TextField is focused:

import SwiftUI

let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)

struct ContentView : View {
    @State private var textFieldString: String = ""

    @State private var editingMode: Bool = false

    var body: some View {
        VStack {
            SecureField($textFieldString, placeholder: Text("My placeholder"))
            }
            .padding()
            .background(lightGreyColor)
            .cornerRadius(10.0)
            .padding()
            .offset(y: editingMode ? -150 : 0)
    }
}

But how to achieve the same with a SecureField in SwiftUI which doesn't have a onEditingChanged parameter?

struct ContentView : View {
    @State private var textFieldString: String = ""

    @State private var editingMode: Bool = false

    var body: some View {
        VStack {
            SecureField($textFieldString, placeholder: Text("My placeholder"))
            }
            .padding()
            .background(lightGreyColor)
            .cornerRadius(10.0)
            .padding()
            .offset(y: editingMode ? -150 : 0)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Womble Tristes
  • 393
  • 1
  • 4
  • 14
  • 7
    There is no **onEditingChanged** for SecureField. But there is for TextField. Don't know why? – Bagusflyer Sep 11 '19 at 16:52
  • did we figure this out, i'm having the same issue – MattBlack Nov 04 '19 at 14:14
  • @Andreas why dont you observe the keyboard will show publisher? – user832 May 31 '20 at 19:54
  • Let me clarify something: focus and editing are two different things. There are two kinds of focus: hardware keyboard focus, and accessibility focus. Apple's definition of focus is that focus lets the user select what they want to activate before actually activating it. When Accessibility > Full Keyboard Access is ON, you can use the keyboard to focus a text field, before you start editing it. In fact you can focus on a different field than the one being edited. – scaly Sep 23 '20 at 00:46

2 Answers2

1

SwiftUI have no Method of focused/unfocused. You need to achieve this using @state and @observablle and onTapGesture. But onTapGesture call Every time when SecureField is taped.

SecureField($textFieldString, placeholder: Text("My placeholder"))
            }
.onTapGesture {
//here apply condition

}
X Body
  • 19
  • 3
  • 3
    If the user is using a hardware keyboard, they can focus a field without editing it AND without tapping on it. So this answer is kind of wrong. – scaly Sep 23 '20 at 00:43
0

You could continue using the UITextField as before, and toggle its secureTextEntry field, instead of using the SecureField component.

flannerykj
  • 91
  • 6