Post

Replies

Boosts

Views

Activity

Update profile name field after closing edit sheet (on separate views)
Hi, I have been trying to make a profile page with an edit button that allows the user to change their name, bio, etc. When I close the edit window, the name does not update. However, if I restart the app, the name changes to the updated (and @AppStorage stored) name variable. How do I make the string containing "Hi, I'm (savedName variable)" update after changing it in the edit window? Here's my code: struct ProfilePage: View { @State var showingSheet = false var body: some View { VStack { HStack { Text("Hi, I'm \(ProfileEditPage().savedName)") .font(.title) Spacer() Button(action: { self.showingSheet.toggle()}) { Text("Edit") } .sheet(isPresented: $showingSheet) { ProfileEditPage() } } .padding(/@START_MENU_TOKEN@/.top, 50.0/@END_MENU_TOKEN@/) Divider() .overlay(.green) HStack { Text("Bio") .font(.title) .padding(/@START_MENU_TOKEN@/.top, 20.0/@END_MENU_TOKEN@/) Spacer() } HStack { Text("temp text") .padding(/@START_MENU_TOKEN@/.top, -10.0/@END_MENU_TOKEN@/) Spacer() } Spacer() } .padding(/@START_MENU_TOKEN@/.horizontal, 40.0/@END_MENU_TOKEN@/) } }
0
0
277
Dec ’22
Begin typing in a textfield after enabling editing with a button press.
Hi, I have a text field for storing the name of a user on their profile. I have an edit button to the right of the text field that enables editing so the user doesn't accidentally change their profile. However, when I enable editing, it just enables the text field but doesn't have the user start typing in it (i.e. bring up the keyboard so that they can edit the text field). Here is my code: struct ProfilePage: View { @State private var name = "" @State var nameEditEnabled = false @AppStorage("NAME_KEY") var savedName = "" var body: some View { VStack { HStack { TextField("Name", text: $savedName) .textFieldStyle(.roundedBorder) .font(.title) .disableAutocorrection(true) .onChange(of: name) {text in self.savedName = name } .onSubmit { nameEditEnabled = false } .disabled(nameEditEnabled ? false : true) Button(action: {self.nameEditEnabled.toggle()}) { Image(systemName: "square.and.pencil") } } } .padding() } } How would I go about making it so that when the button is pressed, the user automatically is put into the textfield and the keyboard pops up so that they can begin typing (without having to manually press the textfield)? Thanks.
0
0
282
Dec ’22
Animate transition between views of TabView in SwiftUI
Hi, I have been trying for many hours to animate the transition between tabs on the TabView for my app in swiftUI. I have looked at many online resources but they are all outdated. The main method I have tried is this: @State private var selection: Tab = .home //tab variable from list of enums not shown TabView(selection: $selection) { // content } .accentColor(//Color) //Works just fine .animation(.easeInOut(duration: 0.3), value: selection) //Does nothing on preview or simulator .transition(.slide) //Does nothing on preview or simulator I really want the animation to work as it would make the app exactly how I would want it. Here is how it looks: Instead of having smooth transition between each tab, its a jarring instantaneous change. I would like to know how to slide from one screen to the other (without using PageView, as that removes the tabview that I want at the bottom). Thanks.
2
0
4.6k
Dec ’22