Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 819
Igor R. Avatar asked Oct 04 '20 11:10

Igor R.


1 Answers

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)
like image 90
Asperi Avatar answered Nov 14 '22 21:11

Asperi