alamodey
alamodey

Reputation: 14953

Rename Cancel button on a modal sheet

I currently invoke an Example Sentence modal sheet by toggling $showingModal on a certain condition.

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

According to the Human Interface Guidelines, I am able to rename Cancel to something more appropriate such as Dismiss. If I was using UIKit instead of SwiftUI, I'd be using the presentController function which then explains that I can rename the Cancel button using setTitle. https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1619560-presentcontroller

But if I'm using SwiftUI, is it possible to rename the Cancel button?

EDIT: Sorry I should have clarified that this is a watchOS app. By default it creates a Cancel button in the top left corner but I just want to rename it to dismiss.

Upvotes: 1

Views: 564

Answers (1)

CodeChimp
CodeChimp

Reputation: 4835

You can do that by using a NavigationView within your ExampleSentences view and adding a toolbar to it, something like this.

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")
                    }
                }
            }
        }
    }
}

Upvotes: -1

Related Questions