Post

Replies

Boosts

Views

Activity

Reply to Support for new ScrollView APIs on iOS 16 and earlier
No is the short answer. If the documentation says iOS 17+, it means iOS 17+. While you could be thinking that Apple could just use the @backDeployed attribute, it’s not that simple. It can only be applied to functions and computed properties and since the new scroll APIs require new types to work, it can’t be used. This is usually how each release of SwiftUI has been: new features are only compatible from that version onwards. You might hope that this could change with the official release later this year, but it’s unlikely.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’23
Reply to Section collapse triangle for section header no longer supported with NavigationStack?
You are correct in that you have to use the new initialisers with the isExpanded parameter to enable the expansion of sections. However, as of iOS 17 beta 1, this only works for lists with the sidebar style. Having spoken with an Apple engineer, it was agreed that this is not the intended behaviour and all list styles should have expandable sections. I did file a feedback report (FB12277169) so this should be resolved soon.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’23
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
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 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 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 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 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