Skip to content

Instantly share code, notes, and snippets.

@HugoSay
Created September 25, 2020 05:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HugoSay/85ad6255c29de81fc56c2a413125fa47 to your computer and use it in GitHub Desktop.
Save HugoSay/85ad6255c29de81fc56c2a413125fa47 to your computer and use it in GitHub Desktop.
Basic swiftUI editable profile using EditMode environment var
import SwiftUI
import MapKit
struct Profile {
var firstName: String
var lastName: String
var city: String
}
struct BasicEditableProfileView: View {
@State var profile: Profile
@State var mode: EditMode = .inactive
@Namespace private var namespace
var body: some View {
NavigationView {
Group {
if mode.isEditing {
editFormView()
} else {
formView()
}
}
.navigationTitle("Profile")
.navigationBarItems(trailing: EditButton())
.environment(\.editMode, self.$mode)
}
}
fileprivate func formView() -> some View {
return Form{
Section(header: Text("My info")) {
HStack {
Text("First name:")
Spacer()
Text(profile.firstName).foregroundColor(.secondary)
}
HStack {
Text("Last name:")
Spacer()
Text(profile.lastName).foregroundColor(.secondary)
}
HStack {
Text("City:")
Spacer()
Text(profile.city).foregroundColor(.secondary)
}
}
}
}
fileprivate func editFormView() -> some View {
return Form{
Section(header: Text("First name")) {
TextField("first name",text: $profile.firstName)
}
Section(header: Text("Last name")) {
TextField("Last name",text: $profile.lastName)
}
Section(header: Text("City")) {
TextField("City",text: $profile.city)
}
}
}
}
struct SwiftUIBasics_Previews: PreviewProvider {
static var previews: some View {
BasicEditableProfileView(profile: Profile(firstName: "Tim", lastName: "Cook", city: "Cupertino"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment