Post

Replies

Boosts

Views

Activity

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
Reply to MultipeerConnectivity data Convert to Dictionary
Instead of sending a String I would send a message (MyMessage) where you can include a type, for example: public struct MyMessage: Codable { var type: Int // 0=word, 1=answer var data: String // the string value public init(type: Int, data: String) { self.type = type self.data = data } } You will have to encode/decode this, then you can find what type of message you received: func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { if let response = try? MyMessage.decode(from: data) { DispatchQueue.main.async { if response.type == 0 { self.dictionaryValues["word"] = response.data } else { self.dictionaryValues["answer"] = response.data } } } else { // deal with no data } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to MultipeerConnectivity data Convert to Dictionary
Yes, as I said, you have to encode/decode this. Try this: extension Encodable { func encode(with encoder: JSONEncoder = JSONEncoder()) throws - Data { return try encoder.encode(self) } } extension Decodable { static func decode(with decoder: JSONDecoder = JSONDecoder(), from data: Data) throws - Self { return try decoder.decode(Self.self, from: data) } } func randomWordSession() { guard let word = try? MyMessage(type: 0, data: dictionaryValues["word"] ?? "").encode() else { return } ... } func answerSession() { guard let answer = try? MyMessage(type: 1, data: dictionaryValues["answer"] ?? "").encode() else { return } ... }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to SwiftUI macOS: tracking the mouse in a view?
nice solution and article from SwiftUI-Lab. It seems you could achieve something similar with just the following: (let me know if this works for you) struct ContentView: View { var mouseLocation: NSPoint { NSEvent.mouseLocation } @State var overImg = false var body: some View { Image(systemName: "clock").resizable().frame(width: 444, height: 444) .onHover { over in overImg = over } .onAppear(perform: { NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) { if overImg { print("mouse: \(self.mouseLocation.x) \(self.mouseLocation.y)") } return $0 } }) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’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:
Replies
Boosts
Views
Activity
Mar ’21
Reply to Get Beginning of Today in SwiftUI
glad it helped. As for the Core Data Fetch Request question, show us the code you are using.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’21
Reply to Differentiating between "return" and focus loss in TextField onCommit
definitely things have changed in macOS 11.3 beta. This is what I get when I start the app, click in the 1st text field, then click in the 2nd text field. I never get any "commit" unless I press the return key. changed 1 changed 1 changed 2
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Mar ’21
Reply to SwiftUI bug: Alert shows twice?
Just to let you know, I tested your code as a macOS app, using xcode 12.5-beta on macos 11.3-beta and all is working well for me with or without the DispatchQueue.main.async.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’21
Reply to Wrong locale display text of DatePicker with GraphicalDatePickerStyle style
have a look at this answer on Stack Overflow: https://stackoverflow.com/questions/66301334/swiftui-datepicker-day-of-week-language
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to Expected Expression Error Swiftui
you have the ") - Error Here }" the wrong way around. It should be: navigationBarItems(trailing: Button(action: { print("Hello") }) { Image(systemName: "square.and.pencil") .resizable() .frame(width: 50, height: 50) }) - Error not Here
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to MultipeerConnectivity data Convert to Dictionary
Instead of sending a String I would send a message (MyMessage) where you can include a type, for example: public struct MyMessage: Codable { var type: Int // 0=word, 1=answer var data: String // the string value public init(type: Int, data: String) { self.type = type self.data = data } } You will have to encode/decode this, then you can find what type of message you received: func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { if let response = try? MyMessage.decode(from: data) { DispatchQueue.main.async { if response.type == 0 { self.dictionaryValues["word"] = response.data } else { self.dictionaryValues["answer"] = response.data } } } else { // deal with no data } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to Does anyone know how to save UIimage in png format to the library with swift UI?
how about just this: let img = UIImage(...) img.pngData() // the data to be saved as png
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to MultipeerConnectivity data Convert to Dictionary
Yes, as I said, you have to encode/decode this. Try this: extension Encodable { func encode(with encoder: JSONEncoder = JSONEncoder()) throws - Data { return try encoder.encode(self) } } extension Decodable { static func decode(with decoder: JSONDecoder = JSONDecoder(), from data: Data) throws - Self { return try decoder.decode(Self.self, from: data) } } func randomWordSession() { guard let word = try? MyMessage(type: 0, data: dictionaryValues["word"] ?? "").encode() else { return } ... } func answerSession() { guard let answer = try? MyMessage(type: 1, data: dictionaryValues["answer"] ?? "").encode() else { return } ... }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to SwiftUI macOS: tracking the mouse in a view?
this is untested, you could try something like this: Image(...).highPriorityGesture( DragGesture(minimumDistance: 1, coordinateSpace: .global) .onChanged { let theCGPoint = $0.location ... })
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to SwiftUI macOS: tracking the mouse in a view?
nice solution and article from SwiftUI-Lab. It seems you could achieve something similar with just the following: (let me know if this works for you) struct ContentView: View { var mouseLocation: NSPoint { NSEvent.mouseLocation } @State var overImg = false var body: some View { Image(systemName: "clock").resizable().frame(width: 444, height: 444) .onHover { over in overImg = over } .onAppear(perform: { NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) { if overImg { print("mouse: \(self.mouseLocation.x) \(self.mouseLocation.y)") } return $0 } }) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’21
Reply to A bug in Apple tutorial? Or what I'm doing wrong?
you are missing these frameworks: import SwiftUI import CoreLocation
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’21