避免在 SwiftUI 中设置按钮样式 [英] Avoid button styling its content in SwiftUI

查看:31
本文介绍了避免在 SwiftUI 中设置按钮样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apple 的 SwiftUI 框架创建一个 iOS 应用程序.因为我需要检测用户是否点击了屏幕的特定区域,所以我显然使用了一个按钮.

I'm creating an iOS app using Apple's SwiftUI framework. As I need to detect if the user taps on a specific area of the screen, I obviously use a button.

问题在于该区域包含一个图像和一个文本,并且当按钮自动为其内容赋予蓝色时,图像也被着色,因此它不是图像而是一个蓝色圆角矩形.

The problem is that the area contains an Image and a Text, and as the button automatically gives its content the blue color, the image is also colored, so instead of being an Image it's just a blue rounded rectangle.

据说一张图片值一千个字,我不擅长解释,这里有一个图解说明会发生什么:

It is said that an image is worth a thousand words, and as I'm not good at explaining, here you have a graphic demonstration of what happens:

按钮外(无按钮样式)

按钮内部(带有按钮样式)

发生这种情况是因为按钮将 .foregroundColor(.blue) 添加到图像中.

This happens because the button is adding .foregroundColor(.blue) to the image.

如何避免/禁用按钮向其组件添加样式?

How can I avoid/disable the button adding style to its components?

编辑:这是我的按钮代码:

ContentView.swift:

Button(action: {/* other code */}) {
                    PackageManagerRow(packageManager: packageManagersData[0])
                }

PackageManagerRow.swift:

struct PackageManagerRow : View {
    var packageManager : PackageManager

    var body: some View {
        VStack {
            HStack {
                Image(packageManager.imageName)
                    .resizable()
                    .frame(width: 42.0, height: 42.0)
                Text(verbatim: packageManager.name)
                Spacer()
                Image(systemName: "checkmark")
                    .foregroundColor(.blue)
                    .opacity(0)
            }.padding(.bottom, 0)
            Divider()
                .padding(.top, -3)
        }
    }
}

推荐答案

我认为这是来自您正在使用的图像的渲染模式.

I think this is from the rendering mode for the image you are using.

你有Image("Cydia logo")(或其他)的地方.

Where you have Image("Cydia logo") (or whatever).

您应该设置渲染模式,例如...

You should be setting the rendering mode like...

Image("Cydia Logo").renderingMode(.original)

这篇关于避免在 SwiftUI 中设置按钮样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆