【问题标题】:Why is my NavigationLink grey, SwiftUI, Mac OS为什么我的 NavigationLink 是灰色的,SwiftUI,Mac OS
【发布时间】:2021-09-10 20:06:14
【问题描述】:

当我将它包装到这样的 NavigationLink 中时,为什么我的 Cell 是灰色的?

NavigationLink(
                destination: Einstellungen_Daten(),
                label: {
                    EinstellungenKachel(titel: "Daten", beschreibung: "In dieser Sektion der Einstellungen kannst du alles rund um deine Daten konfigurieren", bild: Image(systemName: "folder")).buttonStyle(PlainButtonStyle())
                }).buttonStyle(PlainButtonStyle())

此链接如下所示:

如果我不将 EinstellungenKachel 包装到 NavigationLink 中,它看起来像这样:

EinstellungenKachel 的代码是这样的:

struct EinstellungenKachel: View {
    
    var titel: String
    var beschreibung: String
    var bild: Image
    
    var body: some View {
        ZStack{
            Color.gray
                .frame(width: 450, height: 450)
                .cornerRadius(45)
            LinearGradient(gradient: Gradient(colors: [Color.red, Color.blue]), startPoint: .leading, endPoint: .trailing)
                .mask(bild.font(.system(size: 350)))
            RoundedRectangle(cornerRadius: 0)
                .foregroundColor(Color.init(red: 0, green: 0, blue: 0, opacity: 0))
                .overlay(textOverlay, alignment: .leading)
                .cornerRadius(45)
        }.frame(width: 450, height: 450)
        .padding()
    }
    
    var textOverlay: some View {
        VStack{
            Text(titel)
                .font(.custom("Impact", size: 50))
                .padding()
            Text(beschreibung)
                .font(.custom("Impact", size: 25))
                .multilineTextAlignment(.center)
                .padding()
        }
    }
}

请有人帮助我吗? 谢谢,布托什。

【问题讨论】:

    标签: swift swiftui-navigationlink swiftui-button


    【解决方案1】:

    您缺少NavigationView

    考虑这个不起作用的例子:

    struct ContentView: View {
        var body: some View {
            NavigationLink("NavigationLink", destination: Text("Destination"))
        }
    }
    

    在其周围添加NavigationViewNavigationLink 现在将突出显示以表明它不再被禁用 - 因为它实际上可以被点击以导航到某个目的地:

    struct ContentView: View {
        var body: some View {
            NavigationView {
                NavigationLink("NavigationLink", destination: Text("Destination"))
            }
        }
    }
    
    Without NavigationView With NavigationView

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多