SwiftUI - 无法更改图像图标的颜色
问题描述:
我正在尝试构建自己的自定义标签栏视图,但在构建自定义按钮时,我无法更改 Image() 的颜色.
I am trying to build my own custom tabbar view, while building my custom buttons I am unable to change the color of Image().
struct TabBarButton: View {
let title: String
let icon: String
var body: some View {
return GeometryReader{ geometry in
VStack {
Image(self.icon)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: geometry.size.width/2, height: CGFloat(25))
.foregroundColor(.white)
Text(self.title)
.font(.system(size: 8))
.foregroundColor(Color.white)
}
}
}
}
我尝试过 foregroundColor(Color.white)、accentColor(Color.white) 和一些不同的颜色乘数.除了默认的黑色之外,是否有其他颜色没有其他原因?简单的修复只是获得白色图标,但希望我现在可以解决这个问题.
I have tried foregroundColor(Color.white), accentColor(Color.white), and some different color multipliers. Is there a reason the color isn't anything other than default black? Easy fix is just to get white icons but was hoping I could solve this for now.
答
我不确定你想达到什么目的,但可能你只需要模板渲染模式,比如
I'm not sure what are you trying to to achieve, but probably you just need template rendering mode, like
Image(self.icon)
.renderingMode(.template)
.foregroundColor(.white)