简体   繁体   English

如何关闭 SwiftUI 中的 NavigationLink 覆盖颜色?

[英]How to turn off NavigationLink overlay color in SwiftUI?

I've designed a "CardView" using ZStack in which the background layer is a gradient and the foreground layer is a PNG(or PDF) image(the image is a yellow path(like a circle) drawn in Adobe Illustrator).我使用 ZStack 设计了一个“CardView”,其中背景层是渐变层,前景层是 PNG(或 PDF)图像(图像是在 Adobe Illustrator 中绘制的黄色路径(如圆圈))。

When I put the ZStack inside a NavigationLink the gradient remains unchanged and fine, but the image get a bluish overlay color (like default color of a button) therefore there is no more yellow path(the path is bluish).当我将 ZStack 放在 NavigationLink 中时,渐变保持不变并且很好,但是图像的覆盖颜色为蓝色(如按钮的默认颜色),因此不再有黄色路径(路径为蓝色)。

How can get the original color of foreground PNG(or PDF) image?如何获得前景PNG(或PDF)图像的原始颜色?


import SwiftUI

struct MyCardView : View {
    let cRadius : CGFloat = 35
    let cHeight : CGFloat = 220
    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("Hello")) {
                ZStack {
                    RoundedRectangle(cornerRadius: cRadius)
                        .foregroundColor(.white)
                        .opacity(0)
                        .background(LinearGradient(gradient: Gradient(colors: [Color(red: 109/255, green: 58/255, blue: 242/255),Color(red: 57/255, green: 23/255, blue: 189/255)]), startPoint: .leading, endPoint: .trailing), cornerRadius: 0)
                        .cornerRadius(cRadius)
                        .frame(height: cHeight)
                        .padding()
                    Image("someColoredPathPNGimage")
                }
            }
        }
    }
}

The navigationLink acts like Button and it gets the default button style with blue color. navigationLink作用类似于Button ,它使用蓝色的默认按钮样式。

Using .renderingMode(.original) works only on Image views.使用.renderingMode(.original)仅适用于Image视图。 what if you decide to load your Image using some libs or pods?!如果你决定使用一些库或豆荚加载你的图像怎么办?!

It is better to change the default button style to plain using the bellow modifier:最好使用波纹管修饰符将默认按钮样式更改为普通样式:

NavigationLink(destination: Text("Hello")) {
            ZStack {
                RoundedRectangle(cornerRadius: cRadius)
                    .foregroundColor(.white)
                    .opacity(0)
                    .background(LinearGradient(gradient: Gradient(colors: [Color(red: 109/255, green: 58/255, blue: 242/255),Color(red: 57/255, green: 23/255, blue: 189/255)]), startPoint: .leading, endPoint: .trailing), cornerRadius: 0)
                    .cornerRadius(cRadius)
                    .frame(height: cHeight)
                    .padding()
                Image("someColoredPathPNGimage")
            }
        }
        .buttonStyle(PlainButtonStyle())  /*Here, is what you need*/

Try:尝试:

Image("someColoredPathPNGimage").renderingMode(.original)

If your problems continue, consider uploading a screenshot so we get an idea of what you mean.如果您的问题仍然存在,请考虑上传屏幕截图,以便我们了解您的意思。 If you can include the image you are using, even better, so we can replicate.如果您可以包含您正在使用的图像,那就更好了,这样我们就可以复制。

Add .buttonStyle(PlainButtonStyle()) to the NavigationLink(....).buttonStyle(PlainButtonStyle())添加到 NavigationLink(....)

NavigationLink(
   destination: Text("Destination"),
   label: {
       Text("Click Here!")
   }
)
.buttonStyle(PlainButtonStyle())

You change colour with accentColor modifier您使用 accentColor 修饰符更改颜色

 NavigationLink(
   destination: Text("Destination"),
   label: {
       Text("Click Here!")
   }
)
.accentColor(Color.black)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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