Created
September 25, 2020 05:03
-
-
Save HugoSay/85ad6255c29de81fc56c2a413125fa47 to your computer and use it in GitHub Desktop.
Basic swiftUI editable profile using EditMode environment var
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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