
[ad_1]
with parameters and detail
A modifier you can call a. can apply to View
or another ViewModifier
,
Scene modifiers essentially produce another version of the original scene (a new version of the scene is returned each time a modifier is applied).
This makes it possible to add multiple padding and background colors to a single view.

let’s make our own custom ViewModifier
,
we will make a custom ViewModifier
it will make a View
Like the card by adding 3 existing modifiers (Background, CornerRadius, and Shadow).

make a group of Modifiers
and add a new swift file named CardModifier.swift
,

we need to make one struct
which corresponds to ViewModifier
Make a draft This protocol ensures that struct
contains a function body()
which allows us to use struct
As a modifier to our thoughts.
body()
get function Content
(view being modified) Then we modify it (add corner radius, shadow, etc) and return it.
here is the code for CardModifier
,
To use our newly created CardModifier
add .modifier()
modifiers (sorry about the redundancy) and initialize the structure inside.

what .modifier()
Applies all the modifiers defined in our CardModifier
and return the new view. So it’s like we were adding them right there but without having to type the code every time!

When you’re going to use the same set of modifiers in different scenes, use a custom view modifier.
We have all the flexibility in the world. We can create and implement more complex ideas CardModifier
all the same.

We can make our custom view modifiers more flexible by adding parameters.
add one backgroundColor
property for our existing CardModifier
, Make it white by default (if no background color was specified).
Now we can optionally set a custom background color for us CardModifier
,

Wouldn’t it be a good idea to use autocompletion for your custom view modifiers?
Let’s do it.
create a group for Extensions
and add a new swift file named View+Extensions.swift

we’re going to make a modifier .card()
Together Alternative Background color as a parameter.
Add the following code:
now we can use .card()
to use our CardModifier
,

[ad_2]
Source link
#Create #Custom #View #Modifier #SwiftUI #Ricardo #Montemayor #August