重命名模式表上的“取消”按钮

问题描述

我目前通过在特定条件下切换$ showingModal来调用示例语句模式表。

.sheet(isPresented: $showingModal) {
  ExampleSentence()
 
}

根据人机界面指南,我能够将“取消”重命名为更合适的名称,例如“关闭”。如果我使用的是UIKit而不是SwiftUI,那么我将使用presentController函数,该函数然后说明可以使用setTitle重命名“取消”按钮。 https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1619560-presentcontroller

但是,如果我使用的是SwiftUI,是否可以重命名“取消”按钮?

编辑:对不起,我应该澄清这是一个watchOS应用程序。认情况下,它会在左上角创建一个“取消”按钮,但我只想重命名以将其关闭

解决方法

您可以通过在ExampleSentences视图中使用NavigationView并向其中添加toolbar来做到这一点。

struct ExampleSentence: View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {

        NavigationView {
            VStack {
                Text("Hello View")
            }
            .navigationBarTitle("Example Sentences")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .primaryAction) {
                    Button(action: {
                        self.presentationMode.wrappedValue.dismiss()

                    }) {
                        Text("Done")
                    }
                }
            }
        }
    }
}