Post

Replies

Boosts

Views

Activity

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 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 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 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 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 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 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 Text expression in two nested ForEach loop cannot compile
it could be the line with the ".position(...)". Here it is mixing Int and CGFloat. Try this: .position(x: CGFloat(j) * w / 9.0 + w / 18.0, y: CGFloat(i) * h / 9.0 + h / 18.0)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Jun ’21
Reply to How can I handle NavigationLink for HStack row
Since you don't ask a question here, nor provide sufficient code, I can only guess. For NavigationLink to work you need a NavigationView. There is no NavigationView in ContentView, maybe you have one in Home?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to How can I handle NavigationLink for HStack row
yes, if you read my comment carefully, I say in ContentView not ContentView_Previews. Try putting NavigationView in ContentView like @Claude31 answer.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to How to change swiftUI navigation bar background on macOS
try this: NavigationLink("world", destination: Text("world!")).listRowBackground(Color.green)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to MapAnnotations move when Keyboard is opened
the problem goes away for me, when you remove ".ignoresSafeArea(.keyboard, edges: .bottom)". Note that there is no real need for the GeometryReader here.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Jun ’21
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:
Replies
Boosts
Views
Activity
Jun ’21
Reply to AsyncImage - Cancelled Loading before View is Visible
"...but I cannot see what.", we cannot see it either if you don't show us some code. "Are there any tips to investigate this further?, yes show us the code you are using.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Is List re-ordering broken on macOS 11 and 12?
could you try adding ".fixedSize()" to the HStack in ListView. HStack { .. }.fixedSize()
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to A weird problem of LazyVGrid layout
it seems to work for me, if you remove ".padding()" on the LazyVGrid.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21