TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

View composition: Which modifiers can be used on a custom view, and in which order?

Forums > SwiftUI

In the View Composition chapter we can see how we can use .foregroundColor(.blue) on our custom CapsuleText view in our main ContentView's body.

However, calling .fontWeight(.heavy) on it wouldn't work because:

Value of type 'CapsuleView' has no member 'fontWeight'

Likewise, this would be a valid view definition:

struct MyCustomView: View {
    var text: String

    var body: some View {
        Text(text)
            .font(.largeTitle)
            .fontWeight(.heavy)
            .padding()
    }
}

But if we swapped the order of the last two modifiers so that .padding() came before .fontWeight(.heavy), we'd get

Value of type 'some View' has no member 'fontWeight'

So how do we know which modifiers can be used in which order on which views, other than just blind trial and error? How can I understand this more deeply?

3      

Not an answer, I'm afraid, but I would normally suggest checking the documentation - it's not yet very good, though, as you might already have discovered. It can help a little bit if you use the right-hand column of XCode to see Quick Help and to see the Attributes Inspector (you can get to either of these via CMD-click on the component as well) HTH

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.