Post

Replies

Boosts

Views

Activity

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 Using Text(_ : Date, style: DateStyle) in ScrollView causes crash
works well for me on macos 12.beta, xcode 13.beta, target ios 14.7 and macCatalyst 12. Tested on macOS 12 and iPhone ios 14.7. You could try "Text(Date() + 60, style: .relative)" struct ContentView: View { var body: some View { ScrollView { Text(.init() + 60, style: .relative) Text(.init() + 60, style: .date) Text(.init() + 60, style: .time) Text(.init() + 60, style: .timer) Text(.init() + 60, style: .offset) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Repeating function in SwiftUI
you could try this: struct ContentView: View { var body: some View { Text("keep calling a function").padding() .onAppear { callFunc() } } func callFunc() { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { print("-----> callFunc") callFunc() } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to In SwiftUI, any way to intercept each character entered in a TextField?
yes you can, try this: (newVal will give it to you) struct ContentView: View { @State var theText = "" var body: some View { TextField("type something", text: $theText) .onReceive(theText.publisher) { newVal in // do your checks here on newVal print("---> theText: \(theText) newVal: \(newVal) ") } } } you can also try this: TextField("type something", text: $theText) .onChange(of: theText) { newVal in if let lastChar = newVal.last { // do your check here on lastChar print("--> last char: \(lastChar)") } } here is another way: TextField("type something", text: Binding( get: { theText }, set: { newVal in if let lastChar = newVal.last { // do your checks here on lastChar print("--> last char: \(lastChar) newVal: \(newVal) theText: \(theText)") theText = newVal // if pass the checks } }))
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Break Line
Cannot reproduce the issue. Works for me on macos 12.beta, xcode 13.beta, target ios 14.7 and macCatalyst 12. Tested on macOS 12 and iPhone ios 14.7. import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State var test = "My text \nhas a break line" let test2 = "My text \nhas a break line" var body: some View { VStack (spacing: 30) { Text(test) Text(test.description) Text("\(test)") Text(test2) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to SwiftUI Undo/Redo Buttons for TextEditor/Keyboard
I would try something like this: import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { var body: some View { TextEditorView() } } struct TextEditorView: View { @State private var fullText: String = "" @State private var undoText: [Character] = [] var body: some View { VStack { HStack { Button(action: undoType) {Image(systemName:"arrow.uturn.backward")} Button(action: redoType) {Image(systemName:"arrow.uturn.forward")} } TextEditor(text: $fullText) } } func undoType(){ if let lastChar = fullText.last { undoText.append(lastChar) fullText = String(fullText.dropLast()) } } func redoType(){ if let lastChar = undoText.last { undoText.removeLast() fullText.append(lastChar) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21