Post

Replies

Boosts

Views

Activity

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
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 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 Reduce boilerplate while using @Query (SwiftData)
I believe it was mentioned in one of the WWDC activities that Query should support returning a single model object. As SwiftData is in its early stages anyway, the engineers are open to any feedback and enhancement requests. I suggest you file a feedback report detailing why you want Query to return a single model object as well as any useful use cases they can tailor to.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’23
Reply to Fullscreen Cover and Sheet Bug ? XCode 15 / iOS 17
While it could be a bug, I think there's another reason. You are attaching the sheet modifier to each button instance, so in your case there could be over 50 sheets ready waiting to be shown. Instead, you could create one sheet and show it when any of the buttons are pressed. Something like this works: struct TestButton: View { @Binding var isPresented: Bool var title: String = "No String" var body: some View { Button { isPresented.toggle() print("tapped") } label: { Text(title) } } } struct TestingBugView: View { @State private var isPresented = false var body: some View { NavigationStack { ScrollView { ScrollView(.horizontal) { LazyHStack { ForEach(1..<55, id: \.self) { num in TestButton(isPresented: $isPresented, title: num.description) } } .padding() .sheet(isPresented: $isPresented) { Text("presented") } } } } } } #Preview { TestingBugView() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’23
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Dec ’22
Reply to TextField's text color is wrong
Steps To Reproduce, Expected Result, Actual Result?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Dec ’22
Reply to [SwiftUI] swipeAction Button .tint doesn't work
The tint(_:) modifier should colour the swipe action buttons. Try moving the swipeActions(edge:allowsFullSwipe:content:) modifier from the HStack to the Link. I have a feeling that the Link is affecting the tint of the buttons.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to SwiftUI: .fullscreenCover doesn't show .navigationTitle
NavigationView was deprecated but was replaced by two new views: NavigationStack (single column) and NavigationSplitView (multiple columns). In your case, you should just use a NavigationStack where you would have otherwise used a NavigationView.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to How to remove space before SwiftUI List Sections?
In iOS 17, there are these two new modifiers which let you customise the spacing between list sections: listSectionSpacing(_:) with CGFloat listSectionSpacing(_:) with ListSectionSpacing In your case you can apply .listSectionSpacing(0) to your List. Note: this is only available from iOS 17.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23
Reply to the header of the last section when using 'plain' ListStyle in SwiftUI
File a feedback report
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23
Reply to When will mapkit for swiftui SDK be available to use in xcode?
The new features will be available as an official release with Xcode 15, which is normally sometime in September. This is when the official releases of iOS/iPadOS/watchOS… also come out. The new macOS version is usually released in the later months.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Jun ’23
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:
Replies
Boosts
Views
Activity
Jun ’23
Reply to Reduce boilerplate while using @Query (SwiftData)
I believe it was mentioned in one of the WWDC activities that Query should support returning a single model object. As SwiftData is in its early stages anyway, the engineers are open to any feedback and enhancement requests. I suggest you file a feedback report detailing why you want Query to return a single model object as well as any useful use cases they can tailor to.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23
Reply to @Observable and @AppStorage
@Observable should only be used on reference types, or to be more precise classes.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23
Reply to SwiftUI: NavigationSplitView only available on Mac OS 13.0 or newer
What version of Xcode are you using? I'm assuming you're using Xcode 14+ because NavigationSplitView comes with Xcode's builtin SDKs, in this case with macOS 13. Because your Mac is of a lower version, the API is not available which is what the error is saying.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23
Reply to Fullscreen Cover and Sheet Bug ? XCode 15 / iOS 17
While it could be a bug, I think there's another reason. You are attaching the sheet modifier to each button instance, so in your case there could be over 50 sheets ready waiting to be shown. Instead, you could create one sheet and show it when any of the buttons are pressed. Something like this works: struct TestButton: View { @Binding var isPresented: Bool var title: String = "No String" var body: some View { Button { isPresented.toggle() print("tapped") } label: { Text(title) } } } struct TestingBugView: View { @State private var isPresented = false var body: some View { NavigationStack { ScrollView { ScrollView(.horizontal) { LazyHStack { ForEach(1..<55, id: \.self) { num in TestButton(isPresented: $isPresented, title: num.description) } } .padding() .sheet(isPresented: $isPresented) { Text("presented") } } } } } } #Preview { TestingBugView() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23