如何在 watchOS 7 的 SwiftUI 中更改模态 sheet 视图中的 "Cancel" 按钮文本?

How to change "Cancel" button text in modal sheet view in SwiftUI on watchOS 7?

我有两个简单的观点:

import SwiftUI

struct ContentView: View {
    @State private var showingModalView = false
    
    var body: some View {
        Button(action: {
            self.showingModalView.toggle()
        }) {
            Text("Show Modal View")
        }.sheet(isPresented: $showingModalView) {
            ModalView()
        }
    }
}

struct ModalView: View {
    var body: some View {
        Text("Modal View")
    }
}

按下“显示模态”按钮时,显示 ModalView

如何在 ModalView 处于活动状态时将文本“取消”更改为其他内容?

这个Cancel实际上是一个导航栏项。您可以使用工具栏将其替换为自己的按钮,例如

struct ContentView: View {
    @State private var showingModalView = false

    var body: some View {
        Button(action: {
            self.showingModalView.toggle()
        }) {
            Text("Show Modal View")
        }.sheet(isPresented: $showingModalView) {
            ModalView()
            .toolbar(content: {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Close") { self.showingModalView = false }
                }
            })

        }
    }
}

您也可以完全隐藏它(并使用自定义方法关闭,例如在 sheet 视图中使用按钮等)

    }.sheet(isPresented: $showingModalView) {
        ModalView()
        .navigationBarHidden(true)

backup