Post

Replies

Boosts

Views

Activity

Reply to navigate to another view Swiftui
You can use a special type of button that is specifically for navigation called NavigationLink. Use it like this: NavigationLink { // destination view to navigation to DetailView() } label: { Image(systemName: "list.dash")      .foregroundColor(.gray) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to How to sort birthdays in swiftUI so that next birthday from today is on top of the list?
I have just created a playground to test this code out. It seems to work properly for the dates I have provided. See what you think and whether it works in your project. let calendar = Calendar.current // Manual creation of dates so you can see what the values for each are let components = [     DateComponents(year: 2004, month: 3, day: 26),     DateComponents(year: 1973, month: 7, day: 4),     DateComponents(year: 1992, month: 4, day: 1),     DateComponents(year: 2012, month: 12, day: 23),     DateComponents(year: 1988, month: 9, day: 16) ] let birthdays = components.compactMap(calendar.date) // You would only need this bit in your project let nextBirthdays = birthdays.compactMap {     let components = calendar.dateComponents([.day, .month], from: $0)     return calendar.nextDate(after: .now, matching: components, matchingPolicy: .nextTime) }.sorted(by: <) let nextBirthday = nextBirthdays.first print(nextBirthday) // Optional(2022-12-23 00:00:00 +0000) This part is what you would put in your project: // Wrap up next birthday calculation inside a function func nextBirthday(for birthday: Date) -> Date { let calendar = Calendar.current let components = calendar.dateComponents([.day, .month], from: birthday)     return calendar.nextDate(after: .now, matching: components, matchingPolicy: .nextTime) ?? .distantFuture } // Sorted friends by next birthday first friends.sorted { nextBirthday(for: $0.bday) < nextBirthday(for: $1.bday) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to How deploy Mac app on local computer
You can archive the app and have a copy that stored locally on your computer. In the menu bar, go to Product > Archive In the Archives window that opens, select the version you want and click Distribute App Select Copy App as the distribution method Choose a name and location then click Export Open the folder that you just created and double-click the app file inside to run it
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to Calculation for a Picker
What you need to do is create a computed variable that calculates the speed using the three picker values and your formula. Here's an example of what that would look like: struct ContentView: View {     @State var secondesSelection = 10     @State var dixiemesSelection = 0     @State var pisteSelection = 200     let secondes = Array(10..<60)     let dixiemes = Array(0..<10)     let longueurPiste = [200, 250, 333, 500] // Create computed variable that calculates the speed (in km/h)     var vitesseCalculee: Double {         let tempsTour = Double(secondesSelection) + (Double(dixiemesSelection) / 10)         return (Double(pisteSelection) / 1000) / (tempsTour / 3600)     } // Nicely format that speed (rounding to 3 s.f.)     var formattedVitesseCalculee: String {         "\(vitesseCalculee.formatted(.number.precision(.significantDigits(3)))) km/h"     }     var body: some View {         VStack {             Label("Calcul du temps au tour", systemImage: "stopwatch")             Spacer()             Text("Longueur de la piste")                 .font(.title.bold())                 .foregroundColor(.blue)             Picker("Longueur de la piste", selection: $pisteSelection) {                 ForEach(longueurPiste, id: \.self) {                     Text("\($0) m")                 }             }             .pickerStyle(.wheel)             Text("Temps au tour")                 .font(.title.bold())                 .foregroundColor(.blue)             HStack {                 Text("Secondes")                 Text("Dixièmes")             }             HStack {                 Picker("Secondes", selection: $secondesSelection) {                     ForEach(secondes, id: \.self) {                         Text("\($0)'")                     }                 }                 Picker("Dixièmes", selection: $dixiemesSelection) {                     ForEach(dixiemes, id: \.self) {                         Text("\($0)")                     }                 }             }             .pickerStyle(.wheel)             Spacer()             Text("Vitesse calculée: \(formattedVitesseCalculee)")                 .font(.largeTitle)                 .foregroundColor(.blue)         }     } } Your program had a few warnings when I pasted your code in so I resolved them.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to No exact matches in call to instance method 'scaleEffect' ?
Your issue seems to be caused by a typo on this line: @State private var ismageScale: CGFloat = 1 // typo ^^^^^^^^^^^ I'm assuming it should be imageScale like you have used everywhere else in your program. The error is not as helpful as it could be but it does show you there is something wrong with the call to scaleEffect(_:) – there is no variable named imageScale to use.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to How can I add a second row of buttons in my frame when my hstack is too long in Swift UI?
This kind of layout is a horizontal flow layout, because the rows flow on to the next when they run out of space horizontally. You could start by having a look online for some example code: I know there is one from Apple buried in a sample project. I would also recommend looking into the new Layout protocol introduced in iOS 16 which would help immensely and also make the layout reusable.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to Can A Property In An ObservableObject listen to Another Property? How?
The issue you have is that ToolbarItemProperties is a class, and an instance of that class is stored in an @Published variable inside of an ObservableObject class. This AppData class is expected to watch the toolbarItem1 property for any changes (and then update the UI) but, because of the nature of a class, the difference between one before and after a variable change isn't as easily noticeable. If ToolbarItemProperties was a struct this wouldn't be a problem. Because of this, you will need to manually publish changes to tell SwiftUI in advance that a variable is going to change and to reload the UI. This is done by using the provided property of the ObservableObject protocol: objectWillChange.send() Since you didn't provide any example code, I have created some which shows this working: final class ToolbarItemProperties {     var visible = false     var disabled = false     var icon = "questionmark"     var action = {}     var opacity: Double {         disabled ? 0.5 : 1     } } final class AppData: ObservableObject {     @Published var toolbarItem1: ToolbarItemProperties? } struct ContentView: View {     @StateObject private var appData: AppData     init() {         let appData = AppData()         appData.toolbarItem1 = ToolbarItemProperties()         _appData = StateObject(wrappedValue: appData)     }     var body: some View {         VStack(spacing: 20) {             Text("Hello, World!")                 .opacity(appData.toolbarItem1?.opacity ?? 1)             let toggleBinding = Binding<Bool> {                 appData.toolbarItem1?.disabled == true             } set: { // This line is the key part // Must be before the variable change                 appData.objectWillChange.send()                 appData.toolbarItem1?.disabled = $0             }             Toggle("Is Disabled", isOn: toggleBinding)                 .toggleStyle(.button)         }     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to TextField's text color is wrong
From the small amount of testing I have just done, it seems to be related to the autocorrection behaviour of the text field. The misspelt words (dotted underlined in red, sometimes) seem not to be affected by the colour change, but the rest of the text does. This is definitely a bug and you should file a feedback report about it, regardless. In the meantime, apply this modifier to the TextField and it should be resolved (it is for me): .autocorrectionDisabled()
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Colors according to the value of a variable
You can create a computed property in the Proposal struct that calculates and returns the corresponding colour for its Compatibility property. Here is an example of what that could look like: // When Compatibility is of type Int var CompatibilityColor: Color { // Example of colour calculations // Obviously use your own method switch Compatibility { case 0..<25: return .red case 25..<50: return .orange case 50..<75: return .yellow case 75...100: return .green default: return .gray } } You can then access this colour property from within the UI, like this: Rectangle() .fill(Proposal.CompatibilityColor)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22