Post

Replies

Boosts

Views

Activity

Reply to onDelete change label
Tested @OOPer answer, and it works well. However, let say you are building a language App and need to change "Delete" for a selected language dynamically. I've tried the following test code, but that does not work. How can the language be change in code? struct ContentView: View { @State var items = ["item 1","item 2","item 3"] var body: some View { List() { ForEach(0 .. items.count) { Text(items[$0]) }.onDelete(perform: deleteTasks) } .environment(\.locale, Locale(identifier: "ja")) // -- try to change the language } } func deleteTasks(offsets: IndexSet) { }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to Get Beginning of Today in SwiftUI
this maybe of use: let now = Date() let startOfDay = Calendar.current.startOfDay(for: now) print("startOfDay: \(startOfDay)") You can format the date, like: let formatter = DateFormatter() formatter.dateStyle = .full formatter.timeStyle = .medium print(" now: \(formatter.string(from: now)) \n startOfDay: \(formatter.string(from: startOfDay))")
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to Differentiating between "return" and focus loss in TextField onCommit
Maybe I don't understand the question properly, but I'm not getting a "onCommit" trigger when changing the focus. Using xcode 12.5-beta, macos 11.3-beta, targets ios 14.x, catalyst 11.3 with the following test code: struct ContentView: View { @State var searchQuery1 = "" @State var searchQuery2 = "" var body: some View { VStack { TextField("TextField 1", text: $searchQuery1){ _ in print("changed 1") } onCommit: { print("----- commit 1") } // only when the return key is pressed // another TextField to change focus TextField("TextField 2", text: $searchQuery2, onEditingChanged: { _ in print("changed 2") }, onCommit: { print("commit 2") } ) // a button to change focus Button(action: {print("button action")}) { Text("button") } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to when click list item, open new SwiftUI for every list items in SwiftUI
it's not really clear what you are asking. The following code shows how you could navigate to "different views" from your list. struct ContentView: View { let contacts = [ Settings(imageName: "image1", name: "NumberOne"), Settings(imageName: "image2", name: "NumberTwo"), Settings(imageName: "image3", name: "NumberThree"), Settings(imageName: "image4", name: "NumberFour"), ] var body: some View { NavigationView { List(contacts) { contact in NavigationLink(destination: NumberView(contact: contact)) { ContactRow(contact: contact) } } .navigationBarTitle("Contacts") }.environment(\.colorScheme, .light) } } struct ContactRow: View { let contact: Settings var body: some View { HStack { Image(contact.imageName) .resizable() .aspectRatio(contentMode: .fill) .frame(width: 20, height: 20) VStack(alignment: .leading) { Text(contact.name) .font(.system(size: 21, weight: .medium, design: .default)) } } } } struct Settings: Identifiable { let imageName: String let name: String let id = UUID() } struct NumberView: View { let contact: Settings var body: some View { Text("view: \(contact.name)") } } Is this what you are looking for?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to when click list item, open new SwiftUI for every list items in SwiftUI
you can achieve what you want with the following, but it is not a good idea to structure your code this way. The NumberView should be generic and deal with the different "contact" as required, not a different separate view for different contact. Imagine if you have 100 contacts, then you would need 100 different views. struct NumberView: View { let contact: Settings var body: some View { switch contact.name { case "NumberOne": NumberOneView(contact: contact) case "NumberTwo": NumberTwoView(contact: contact) case "NumberThree": NumberThreeView(contact: contact) case "NumberFour": NumberFourView(contact: contact) default: NumberOneView(contact: contact) } } } struct NumberOneView: View { let contact: Settings var body: some View { Text("NumberOneView: \(contact.name)") } } struct NumberTwoView: View { let contact: Settings var body: some View { Text("NumberTwoView: \(contact.name)") } } struct NumberThreeView: View { let contact: Settings var body: some View { Text("NumberThreeView: \(contact.name)") } } struct NumberFourView: View { let contact: Settings var body: some View { Text("NumberFourView: \(contact.name)") } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21