Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid button styling its content in SwiftUI

Tags:

ios

swift

swiftui

I'm creating an iOS app using Apple's SwiftUI framework. As I need to detect if the user taps on a specific area of the screen, I obviously use a button.

The problem is that the area contains an Image and a Text, and as the button automatically gives its content the blue color, the image is also colored, so instead of being an Image it's just a blue rounded rectangle.

It is said that an image is worth a thousand words, and as I'm not good at explaining, here you have a graphic demonstration of what happens:

Outside the button (without button styling) without button styling

Inside the button (with button styling) with button styling

This happens because the button is adding .foregroundColor(.blue) to the image.

How can I avoid/disable the button adding style to its components?

EDIT: This is my button code:

ContentView.swift:

Button(action: {/* other code */}) {
                    PackageManagerRow(packageManager: packageManagersData[0])
                }

PackageManagerRow.swift:

struct PackageManagerRow : View {
    var packageManager : PackageManager

    var body: some View {
        VStack {
            HStack {
                Image(packageManager.imageName)
                    .resizable()
                    .frame(width: 42.0, height: 42.0)
                Text(verbatim: packageManager.name)
                Spacer()
                Image(systemName: "checkmark")
                    .foregroundColor(.blue)
                    .opacity(0)
            }.padding(.bottom, 0)
            Divider()
                .padding(.top, -3)
        }
    }
}
like image 726
iAlex11 Avatar asked Jul 08 '19 09:07

iAlex11


1 Answers

I think this is from the rendering mode for the image you are using.

Where you have Image("Cydia logo") (or whatever).

You should be setting the rendering mode like...

Image("Cydia Logo").renderingMode(.original)
like image 55
Fogmeister Avatar answered Sep 25 '22 13:09

Fogmeister