UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.