Search code examples
swiftuigradientbackground-color

How to get the background color of the current view in SwiftUI?


I'd like to set the gradient second color to the color of the background.

I tried to set .clear, but for some reason it becomes dark. If I use .white the radient is as expected in light mode. The best for me is to use the background color to handle themes.

So how do I get the background color?

// Fill gradient
                        line
                            .trim(to: animate ? 1 : 0)
                            .fill(
                                LinearGradient(
                                    gradient: Gradient(colors: [
                                        Color(graphColor),
                                        <content backgroundColor>
                                        //.white
                                        //.clear
                                    ]),
                                    startPoint: .top,
                                    endPoint: .bottom
                                )

                            )
                        ```

Solution

  • We can use system background, like

    LinearGradient(
        gradient: Gradient(colors: [
            Color(graphColor),
            Color(uiColor: UIColor.systemBackground)    // << here !!
        ]),
        startPoint: .top,
        endPoint: .bottom
    )