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

I have 2 simple views:

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

When "Show Modal" button pressed, ModalView is show.

How to change text "Cancel" when ModalView is active to something else?

enter image description here



Solution 1:[1]

This Cancel is actually a navigation bar item. You can replace it with own button using toolbar, like

demo1

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

        }
    }
}

also you can hide it at all (and make your custom approach to close, eg. with button in sheet view, etc.)

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

backup

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1