Post

Replies

Boosts

Views

Activity

Reply to How to refresh AsyncImage
yes it does refresh the view/image, it just that your question mentioned that you want the url to be the same, and so the image is the same. If you want a different url when you press a button, then just do this: struct ContentView: View { @State var url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/9/98/B165841.jpg") var body: some View { VStack { Button("click to refresh view") { url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/8/81/PIA24681-1041-Ganymede-JupiterMoon-Juno-20210607.jpg") } AsyncImage(url: url, scale: 1) { image in image .resizable() .aspectRatio(1, contentMode: .fit) .frame(width: 300, height: 400, alignment: .center) } placeholder: { ProgressView() .progressViewStyle(CircularProgressViewStyle(tint: .orange)) .scaleEffect(3) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to How to refresh AsyncImage
Yes this AsyncImage is nice. However if the url does not change, it is pointless to refresh the image/view. If you really need to do it, I sometimes use this trick/hack: struct ContentView: View { @State var url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/9/98/B165841.jpg") @State var refreshMe = false var body: some View { VStack { Button("click to refresh view") { refreshMe.toggle() } AsyncImage(url: url, scale: 1) { image in image .resizable() .aspectRatio(1, contentMode: .fit) .frame(width: 300, height: 400, alignment: .center) } placeholder: { ProgressView() .progressViewStyle(CircularProgressViewStyle(tint: .orange)) .scaleEffect(3) }//.border(refreshMe ? .red : .green) // <--- testing to show the refresh is happening }.accentColor(refreshMe ? .black : .black) // <--- to force a refresh } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to How to use fileImporter to grab URL for later access to file contents
here is a simple example to get the file url from fileImporter: @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State var imported = false @State var fileUrl: URL? var body: some View { VStack (spacing: 30) { Button(action: {imported.toggle()}, label: { Text("Import file") }) if let theUrl = fileUrl { Text("file url is \(theUrl.absoluteString)") } } .fileImporter(isPresented: $imported, allowedContentTypes: [.pdf]) { res in do { fileUrl = try res.get() print("---> fileUrl: \(fileUrl)") } catch{ print ("error reading: \(error.localizedDescription)") } } } }
Topic: App & System Services SubTopic: General Tags:
Jun ’21
Reply to onTapGesture conflicts with button
you could try something like this: struct TextFieldPopupView: View { @State private var text = "" @State private var isON = false @State var buttonTap = false var body: some View { Form { Text("Hello") TextField("Hello", text: $text).textFieldStyle(RoundedBorderTextFieldStyle()) Button("Button"){ buttonTap = true print("Button") }.buttonStyle(BorderlessButtonStyle()) .border(Color.red) Toggle(isOn: $isON, label: { Text("Toggle") }) }.buttonStyle(BorderlessButtonStyle()) .onTapGesture { if buttonTap { // just reset the button tap flag buttonTap = false } else { print("---> Form only tapped") // do Form tap only } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to onTapGesture conflicts with button
here are a few ideas. Since you also have the ".onTapGesture" on the Form itself, it will also be called. So you will have to use some flag to determine which is being called. struct TextFieldPopupView: View { @State private var text = "" @State private var isON = false var body: some View { Form { Text("Hello") TextField("Hello", text: $text).textFieldStyle(RoundedBorderTextFieldStyle()) Button("Button1"){ print("Button1") }.buttonStyle(BorderlessButtonStyle()) Text("Button2").foregroundColor(.blue) .onTapGesture { print("Button2") } Button("Button3"){} .onTapGesture { print("Button3") } Toggle(isOn: $isON, label: { Text("Toggle") }) } .onTapGesture { print("---> Form") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Can not ForEach Int Array in Dictionary
could you try this: struct ContentView: View { @State private var notedNumbers: [Int:[Int]] = [0:[1,2]] var body: some View { ForEach(Array(notedNumbers.keys.enumerated()), id: \.element) { _, key in let row = key / 9 let col = key % 9 if let notedNumbersKey = notedNumbers[key] { ForEach(notedNumbersKey.indices, id: \.self) { i in // Cannot ForEach the Int Array here, compiler cannot type check the expression. Text("---> \(i)") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Dotted Line Border
try this: struct ContentView: View { var body: some View { Rectangle() .stroke(style: StrokeStyle(lineWidth: 2, dash: [5])) .foregroundColor(.red) .frame(width: 111, height: 111) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to How to delete in CoreData in an MacOS SwiftUI application (.onDelete is not working)
The ".onDelete(...)" works on MacOS, iOS and MacCatalyst. Here is a very basic example, using MacOS 11.4, xcode 12.5, target ios 14.5, macCatalyst 11.3 and MacOS 11.4. You have to swipe your mouse right to left. import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State var items = ["1","2","3"] private func deleteItems(offsets: IndexSet) { withAnimation { print("deleteItems") } } private func addItem() { withAnimation { print("addItem") } } var body: some View { List { ForEach(items, id: \.self) { item in Text("Item at \(item)") }.onDelete(perform: deleteItems) } .toolbar { Button(action: addItem) { Label("Add Item", systemImage: "plus") } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’21