Is it possible to add function prompt in custom tool bar viewModifier

127 Views Asked by At

I have created following custom toolbar modifier which I use in number of app screens . The only change on each screen is the function which added to the button! How can I add a function prompt inside the custom toolbar modifier so I can update function for each screen?

Thank You!

Following is the code

import Foundation
import SwiftUI

struct toolbarModifier: ViewModifier {
func body (content: Content) -> some View {
    content
    .toolbar {
        ToolbarItem (placement: .navigationBarTrailing) {
            Button {
                // addItem() function for example
            } label: {
               Image (systemName: "checkmark.circle.fill")
            }
        }
    }
}

}

I tried searching on the website but couldn’t find any answer!

1

There are 1 best solutions below

0
lorem ipsum On BEST ANSWER

You can pass it as a variable

struct ToolbarModifier: ViewModifier { //struct should start with uppercase
    //This is how you declare a function
    let action: () -> Void
    func body (content: Content) -> some View {
        content
            .toolbar {
                ToolbarItem (placement: .navigationBarTrailing) {
                    Button(action: action) { //Use it in the action of the Button
                        Image (systemName: "checkmark.circle.fill")
                        
                    }
                }
            }
    }
}