Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Button Images showing up black

I am trying to make a Button in SwiftUI show an image alongside the text. However, the image is showing up black even though it shows up when not in a button. What edits to my code do I need to add so that the image displays properly.

Here is what my code is currently:

List{
    Button(action: {
        self.changeAppIcon("AlternateIcon5")
    }) {
        HStack {
            Image("AlternateIcon5")
                .resizable()
                .frame(width: 50, height: 50)
                .cornerRadius(10.0)
            Text("Navy Blue")
        }
    }


    HStack {
        Image("AlternateIcon5")
            .resizable()
            .frame(width: 50, height: 50)
            .cornerRadius(10.0)
        Text("Test")
    }
}

Here is what it shows up as:

enter image description here

like image 953
Thomas Braun Avatar asked Nov 13 '19 20:11

Thomas Braun


People also ask

How do I create an image button in SwiftUI?

A Basic Image Button Creating an image button in SwiftUI is extremely straightforward. Right next you can see the code required to make one. Notice that we’re providing an Image view as the button’s label, passing the name of an actual image in the Assets catalog as an argument to it.

How to add gradient background to buttons in SwiftUI?

With SwiftUI, you can easily style the button with gradient background. Not only can you a specific color to the background modifier, you can easily apply a gradient effect for any buttons. All you need to do is to replace the following line of code: With: The SwiftUI framework comes with several built-in gradient effect.

How to design the button with rounded borders in SwiftUI?

What if you want to design the button with rounded borders like this? SwiftUI comes with a modifier named cornerRadius that lets you easily create rounded corners. To render the button’s background with rounded corners, you can simply use the modifier and specify the corner radius like this:.cornerRadius (40)

What is the difference between UIButton and button in SwiftUI?

If you have learned iOS programming before, Button in SwiftUI is very similar to UIButton in UIKit. It’s just more flexible and customizable. You will understand what I mean in a while.


Video Answer


2 Answers

Without Code:

  1. Go to Assets.xcassets from the project navigator and select your image there.
  2. From the right panel, Attributes Inspector
  3. Select Original Image for Render As

Inspector

Remember you should always do this for all images that you don't want to apply any color on them.


With Code:

add this modifier to your Image:

.renderingMode(.original)

Note that it should be right after the Image itself (NOT after other modifiers like cornerRadius)

like image 142
Mojtaba Hosseini Avatar answered Oct 22 '22 09:10

Mojtaba Hosseini


Please add the .renderingMode(.original) right after the Image().

    List{
    Button(action: {
        self.changeAppIcon("AlternateIcon5")
    }) {
        HStack {
            Image("AlternateIcon5")
                .renderingMode(.original)
                .resizable()
                .frame(width: 50, height: 50)
                .cornerRadius(10.0)
            Text("Navy Blue")
        }
    }


    HStack {
        Image("AlternateIcon5")
            .resizable()
            .frame(width: 50, height: 50)
            .cornerRadius(10.0)
        Text("Test")
    }
}
like image 4
E.Coms Avatar answered Oct 22 '22 08:10

E.Coms