¿No puedo usar el interruptor para verificar una variable e implementar una vista basada en el valor de la variable? Intenté usar if else también pero sigo recibiendo el mismo error. ¿Tengo que crear un método y devolver una vista para el mismo y usarlo aquí?
struct AppThemeButton: View { var action: (() -> Swift.Void)? var buttonType: ThemeButtonType = .bordered var body: some View { Button { // button action if let act = action { act() } } label: { Text("+ \(TextStrings.addAProject.localized())") .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center) .background( switch self.buttonType { case .bordered: Color.green case .colored: Color.red } ) .frame(height: 60, alignment: .center) .padding([.leading, .trailing]) } } } enum ThemeButtonType { case bordered case colored }Está utilizando este modificador https://developer.apple.com/documentation/swiftui/view/background(_:alignment:) , requiere una View como parámetro, no una función o cierre.
El modificador está obsoleto desde iOS 15. Si su aplicación está destinada a iOS 15 y superior, puede usar este nuevo modificador https://developer.apple.com/documentation/swiftui/view/background(alignment:content:)
En el caso de iOS 15 inferior, debe envolver su etiqueta con su switcher
enum ThemeButtonStyle: ButtonStyle { case filled case bordered func makeBody(configuration: Configuration) -> some View { switch self { case .filled: configuration.label .foregroundColor(.white) .padding() .background(Capsule().fill(.red)) case .bordered: configuration.label .foregroundColor(.black) .padding() .background(Capsule().stroke(.black)) } } } struct AppThemeButton: View { let style: ThemeButtonStyle var body: some View { Button { print("Touched") } label: { Text("Touch Me") }.buttonStyle(style) } } struct ThemeButtonStyle_Previews: PreviewProvider { static var previews: some View { VStack { AppThemeButton(style: .filled) AppThemeButton(style: .bordered) } } }Resultado