0

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!

Rob
  • 14,746
  • 28
  • 47
  • 65
mzkn
  • 39
  • 6

1 Answers1

0

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")
                        
                    }
                }
            }
    }
}
lorem ipsum
  • 21,175
  • 5
  • 24
  • 48