Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mluisbrown
Created September 22, 2019 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mluisbrown/3a6e1f0e11eb61f0eff3867719caf7b7 to your computer and use it in GitHub Desktop.
Save mluisbrown/3a6e1f0e11eb61f0eff3867719caf7b7 to your computer and use it in GitHub Desktop.
Example view that demonstrates bug with Buttons in Lists in SwiftUI
import SwiftUI
struct ContentView : View {
var body: some View {
return NavigationView {
VStack {
List {
Section(header: Text("Credentials").font(.body).padding([.top, .bottom])) {
HStack {
Text("Email:")
.frame(width: 100, alignment: .leading)
TextField(
"email address",
text: Binding.constant("")
).clipped()
}
PasswordField(
Binding.constant(""),
label: Text("Password:"),
placeholder: "********"
)
PasswordField(
Binding.constant(""),
label: Text("Confirm Password:"),
placeholder: "********"
)
}
}.listStyle(GroupedListStyle())
}.navigationBarTitle(Text("Sign up"))
}
}
}
struct PasswordField: View {
let binding: Binding<String>
let label: Text
let placeholder: String
// @State is used for state that is entirely local to the view
@State var isPasswordVisible = false
init(
_ binding: Binding<String>,
label: Text,
placeholder: String
) {
self.binding = binding
self.label = label
self.placeholder = placeholder
}
var body: some View {
HStack {
label.frame(width: 100, alignment: .leading)
if isPasswordVisible {
TextField(placeholder, text: binding)
.textContentType(.password)
.clipped()
} else {
SecureField(placeholder, text: binding)
.textContentType(.password)
.clipped()
}
Button(
action: { self.isPasswordVisible.toggle() },
label: { Image(systemName: isPasswordVisible ? "eye.slash" : "eye") }
)
}.lineLimit(nil)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment