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:
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.
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.
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)
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.
Assets.xcassets
from the project navigator and select your image there.Original Image
for Render As
Remember you should always do this for all images that you don't want to apply any color on them.
add this modifier to your Image
:
.renderingMode(.original)
Note that it should be right after the Image
itself (NOT after other modifiers like cornerRadius
)
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")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With