0

I am trying to custom Image modifier just like Text modifier:

    struct FlagImage: ViewModifier {
    func body(content: Content) -> some View {
        content
            .renderingMode(.original)
            .clipShape(Capsule())
            .overlay(Capsule().stroke(Color.black, lineWidth: 1))
            .shadow(color: .black, radius: 2)
        }
}

The first error is

Value of type 'FlagImage.Content' (aka '_ViewModifier_Content<FlagImage>') has no member 'renderingMode'

Cannot infer contextual base in reference to member 'original'

and the second error is

Cannot infer contextual base in reference to member 'black'

Why Image modifier content can't directly called .renderingMode(.original)? How can I solve this problem?

1 Answer 1

2

ViewModifier is modifier of View, which is generic view, so its content is also opaque some generic view and you cannot apply specific modifiers to it, like Image.renderingMode.

The solution is make functional modifier directly to Image in extension, like

extension Image {
    func flagImage() -> some View {
        self
            .renderingMode(.original)
            .clipShape(Capsule())
            .overlay(Capsule().stroke(Color.black, lineWidth: 1))
            .shadow(color: .black, radius: 2)
    }
}

Tested with Xcode 11.4 / iOS 13.4

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.