简体   繁体   English

如何在 SwiftUI 中创建带有图像的按钮?

[英]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 中找不到它。

Go to the image and change the Render As "Original Image"转到图像并将渲染更改为“原始图像” 在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM