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

jckbn6z7  于 2022-12-27  发布在  Swift
关注(0)|答案(4)|浏览(192)

我使用ZStack设计了一个“CardView”,其中背景层是一个渐变,前景层是一个PNG(或PDF)图像(图像是一个黄色路径(像一个圆圈)在Adobe Illustrator中绘制)。
当我将ZStack放入NavigationLink中时,渐变保持不变且很好,但图像获得了蓝色覆盖颜色(类似于按钮的默认颜色),因此没有更多黄色路径(路径为蓝色)。
怎样才能得到前景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")
                }
            }
        }
    }
}
y0u0uwnf

y0u0uwnf1#

navigationLink的作用类似于Button,它获得默认的蓝色按钮样式。
使用.renderingMode(.original)只适用于Image视图。如果你决定使用一些库或pod加载你的图像呢?!
最好使用下面的修饰符将默认按钮样式更改为普通样式:

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
oxf4rvwz

oxf4rvwz2#

试试看:

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

如果您的问题仍然存在,请考虑上传截图,以便我们了解您的意思。如果您可以包含您正在使用的图像,那就更好了,这样我们就可以复制。

5lhxktic

5lhxktic3#

.buttonStyle(PlainButtonStyle())添加到导航链接(....)

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

62lalag44#

使用accentColor修饰符更改颜色

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

相关问题