[英]How can I create a button with image in SwiftUI?
I created a button in SwiftUI with these line of codes:我使用以下代码行在 SwiftUI 中创建了一个按钮:
Button(action: {
print("button pressed")
}) {
Image("marker")
}
but marker
image automatically changes to blue color.但
marker
图像会自动变为蓝色。
I want to use original image in button.我想在按钮中使用原始图像。
this is original marker.png
:这是原始的
marker.png
:
but SwiftUI changes it to this:但是 SwiftUI 将其更改为:
I remember we have tintColor or something like this in UIButton
, but I can't find it in SwiftUI.我记得我们在
UIButton
中有 tintColor 或类似的东西,但我在 SwiftUI 中找不到它。
Another way to set programmatically:-另一种以编程方式设置的方法:-
var body: some View {
Button(action: {
print("button pressed")
}) {
Image("marker")
.renderingMode(Image.TemplateRenderingMode?.init(Image.TemplateRenderingMode.original))
}
}
You can try this:你可以试试这个:
var body: some View {
Button(action: {
print("button pressed")
}) {
Image("marker")
.renderingMode(.original)
}
}
SwiftUI
用户界面
var body: some View {
HStack {
Image(uiImage: UIImage(named: "Login")!)
.renderingMode(.original)
.font(.title)
.foregroundColor(.blue)
Text("Login")
.font(.title)
.foregroundColor(.white)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.