选择自定义按钮后,NavigationLink 不会转换到新视图

NavigationLink won't transition to new View upon selecting custom Button

当我将按钮嵌入 NavigationLink 并将目标设置为文本视图时,单击按钮时视图不会转换,但如果我只是随机单击按钮有时会转换。我做错了什么?

这是我正在使用的...

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        NavigationView{
            VStack{
                Spacer()
                VStack(spacing: 50){
                    NavigationLink(destination: Text("Testing")){
                    awButton(content: "Request Support", backColor: Color(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)))
                    }  
                }
                Spacer()
            }
            .navigationBarTitle(Text("AccessWeb"))
            .navigationBarItems(trailing: HStack{
                Image(systemName: "bell")
                    .font(.system(size: 30))
                Image(systemName:"person.circle")
                    .font(.system(size: 30))
                Text("Tester")
                    .font(.system(size: 20))
            })
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}



struct awButton: View {
    var content : String
    var backColor : Color
    var body: some View {
        
        Button(action: {}, label: {
            VStack {
                HStack {
                    Image(uiImage: #imageLiteral(resourceName: "awText"))
                        .resizable()
                        .frame(width: 30, height: 20)
                        .padding(.leading)
                    Spacer()
                }
                .padding(.top)
                HStack {
                    Text("\(content)")
                        .font(.title)
                        .fontWeight(.semibold)
                        .offset(y: 10.0)
                }
                Spacer()
            }
        })
        .foregroundColor(Color(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)))
        .frame(width: 300, height: 150, alignment: .center)
        .background(backColor)
        .clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous))
        
    }
}

NavigationLink 已经将所有内容包装在一个按钮中(或者至少是接收 click/tap 事件的东西),因此 包括 Button 在您的自定义视图中,您正在阻止那些点击事件到达原始 NavigationLink。

但是,由于您的操作是空的,您可以取出 Button 包装器:

struct awButton: View {
    var content : String
    var backColor : Color
    var body: some View {
        
        VStack {
            HStack {
                Image(systemName: "pencil")
                    .resizable()
                    .frame(width: 30, height: 20)
                    .padding(.leading)
                Spacer()
            }
            .padding(.top)
            HStack {
                Text("\(content)")
                    .font(.title)
                    .fontWeight(.semibold)
                    .offset(y: 10.0)
            }
            Spacer()
        }
        .foregroundColor(Color(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)))
        .frame(width: 300, height: 150, alignment: .center)
        .background(backColor)
        .clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous))
        
    }
}