Creating our first custom modifier

For this, we'll need to define another struct. We can do this in the same file as AddRecipeView.swift, but just directly below our main struct. Start by adding the following snippet, and we'll go through it line by line:

struct AddButton: ViewModifier {        @Binding var text: String    @Binding var ingredients: [String]        public func body(content: Content) -> some View {                        ZStack(alignment: .trailing) {            content            Button(action: {                if self.text != "" {                    self.ingredients.append(self.text)                    self.text = ""                }            }) {                Image(systemName: "plus")                    .foregroundColor(Color(UIColor.opaqueSeparator))            }            .padding(.trailing, 8)                    }    }}

We'll start with the declaration; we create a new struct called AddButton that extends from the ViewModifier ...

Get Learn SwiftUI now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.