Skip to main content

How to stack modifiers to create more advanced effects

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to stack modifiers to create more advanced effects 관련

SwiftUI by Example

Back to Home

How to stack modifiers to create more advanced effects | SwiftUI by Example

How to stack modifiers to create more advanced effects

Updated for Xcode 15

Each modifier you add to a view adjusts whatever came before it, and you’re able to repeat modifiers more than once.

For example, we could add padding and a background color around a text view, then add some more padding and a different background color, then add some more padding and a third background color, all to make a particular effect:

Text("Forecast: Sun")
    .font(.largeTitle)
    .foregroundStyle(.white)
    .padding()
    .background(.red)
    .padding()
    .background(.orange)
    .padding()
    .background(.yellow)

Download this as an Xcode projectopen in new window

The text “Forecast: Sun” in white on concentric rectangles colored (from inside outwards) red, orange, and yellow.
The text “Forecast: Sun” in white on concentric rectangles colored (from inside outwards) red, orange, and yellow.

This is particularly useful for things like shadows, where you can create stronger shadow effects by repeating them as often as you need.

Similar solutions…
How to customize stack layouts with alignment and spacing | SwiftUI by Example

How to customize stack layouts with alignment and spacing
How to add advanced text styling using AttributedString | SwiftUI by Example

How to add advanced text styling using AttributedString
How to force views to one side inside a stack using Spacer | SwiftUI by Example

How to force views to one side inside a stack using Spacer
How to create modifiers for a UIViewRepresentable struct | SwiftUI by Example

How to create modifiers for a UIViewRepresentable struct
How to add Metal shaders to SwiftUI views using layer effects | SwiftUI by Example

How to add Metal shaders to SwiftUI views using layer effects

이찬희 (MarkiiimarK)
Never Stop Learning.