我的 SwiftUI 项目中有一个 Swift 包,其中包含 UI 组件的样式。此外,此包还包括一个资源文件夹,其中包含可在样式中使用的资源。我可以通过名称获取图像,但我想知道是否可以使用枚举访问图像。因为 Xcode 15 会自动生成资产符号。
\n下面的例子可以解释我的意思:
\npublic struct CheckboxToggleStyle: ToggleStyle {\n@Binding var color: Color\n\nlet checked = Image("checkbox-checked", bundle: .module) // \xe2\x9c\x85 works\nlet unchecked = Image("checkbox-unchecked", bundle: .module) // \xe2\x9c\x85 works\nlet checked = Image(.checkboxChecked) // \xe2\x9d\x8c failed\nlet unchecked = Image(.checkboxUnchecked) // \xe2\x9d\x8c failed\n\npublic func makeBody(configuration: Configuration) -> some View {\n (configuration.isOn ? checked : unchecked)\n .renderingMode(.template)\n .foregroundColor(color)\n .onTapGesture {\n withAnimation {\n configuration.isOn.toggle()\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n}
\n