Swiftui 单击图像按钮时,在Swift UI中打开新视图

Swiftui 单击图像按钮时,在Swift UI中打开新视图,swiftui,Swiftui,我想在单击SwiftUI中的图像按钮后打开新视图,有什么想法吗 Button(action: {}) { Image("gift") Text("Send") .padding(.horizontal) } .padding() .foregroundColor(.white) .background(Color.gray) .cornerRadius(.infinity) 如果您希望它出现在导航视图中,可以使用导航链接而

我想在单击SwiftUI中的图像按钮后打开新视图,有什么想法吗

Button(action: {}) {
    Image("gift")
    Text("Send")
        .padding(.horizontal)
}
.padding()
.foregroundColor(.white)
.background(Color.gray)
.cornerRadius(.infinity)

如果您希望它出现在
导航视图中
,可以使用
导航链接
而不是
按钮

struct ContentView : View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView()) {
                    Image("gift")
                    Text("Send")
                        .padding(.horizontal)
                .padding()
                .foregroundColor(.white)
                .background(Color.gray)
                .cornerRadius(.infinity)
            }
        }
    }
}

struct DetailView : View {
    var body: some View {
        Text("Detail")
    }
}
如果要显示
工作表
,可以使用
工作表
修饰符和
@State
变量:

struct ContentView : View {
    @State private var sheetPresented = false
    
    var body: some View {
        Button(action: {
            sheetPresented = true
        }) {
            Image("gift")
            Text("Send")
                .padding(.horizontal)
        }
        .padding()
        .foregroundColor(.white)
        .background(Color.gray)
        .cornerRadius(.infinity)
        .sheet(isPresented: $sheetPresented) {
            DetailView()
        }
    }
}

struct DetailView : View {
    var body: some View {
        Text("Detail")
    }
}

这么简单你的问题是什么?