Your First iOS and SwiftUI App · View Modifiers | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/41

SwiftUI inspector is updated. You can scroll to the bottom and select “Add Modifier”. From there you can select Shadow and change values.

I don’t understand the modifier struct.

The function Body, you coded it

return content
.foregoundcolor
.shadow
.font etc…

But I don’t understand the “return content”, wouldn’t Swift hit the return keyword and return from the function?

It’s like the function is written upside down (if you see what I mean).

Thanks,

Ray, I have a question.
In the app you defined a custom view modifier struct called LabelStyle.
struct LabelStyle: ViewModifier {
func body(content: Content) -> some View {
return content.foregroundColor(Color.white)
.shadow(color: Color.black, radius: 5, x: 2, y: 2)
.font(Font.custom(“Arial Rounded MT Bold”, size: 18))
}
}

After that you used this LabelStyle to style/modify the text like below. You created an instance of LabelStyle() and pass it s an argument to modifier function
HStack {
Text(“Put the bullseye as close as you can to:”).modifier(LabelStyle())
Text("(target)")
}
Generally I get all of these but there are a few things I want to understand.

1- In the LabelStyle struct we wrote body function and this function is doing all the styling work and returns the styled view. However when we use LableStyle we only created a instance of it and passed it to modifier function(.modifier(LabelStyle())). We never called body method for the instance we created. How/when/where this body method is called? Could you please explain this with an example?

2- Could we call this body method manually and style a text instead of using modifier method? Is so please could you example?

Thanks

Adding shadows can be done like @sodoherty explained above and you can still find it in the documentation by going to SwiftUI > Views and controls > Text > View modifiers. Do a find for shadows, it will be right under the section for Applying Graphical Effects to a View.

Something weird at this stage of my swiftui bullseye setup. at this point my alert popup is now showing up with yellow background. It was previously just a white background. From reading around, i thought the alert was not customisable in terms of background colors, so i’m thinking i’ve messed up a System setting somewhere??

Anyone come across this before?

Thanks

After some google-searches on the apple.com site for these simulator symptoms, it seems to be related Xcode 12.3, hence nothing related to a swiftui coding mishap or setup. Just assuming, apple will restore the default backgrounds in the next Xcode release.