How to set background color?

Hi,


How do you change the background color in Swift UI?


Best,


P

Replies

Here's how I'm doing it. You call .background and add a shape with a foreground color.


Gray background:

.background(Rectangle()
  .foregroundColor(.gray))


Rounded rect with drop shadow (Shadow color is in my xcassets and is just black at 15% opacity):

.background(RoundedRectangle(cornerRadius: 8)
  .foregroundColor(.white)
    .shadow(color: Color("Shadow"), radius: 8, x: 0, y: 4))

Any ideas to set it on the view itself? In the WWDC Implementing Dark Mode video they mention setting the Background color to the "System Background Color" at about the 7:25 mark. Of course, if you are not using Interface Builder you can't do this.


I managed to put a

.background(Color.red)

in it and verified that it does set the view to red in both light and dark mode, but I am not sure how to use the .background to set the color to the "System Background Color"


Any tips?

Thanks,

Dave

I have not found this either, but I did notice that


.background(Color.primary.colorInvert())


produces the effect we are after, I suspect that there will be a few color choices like primaryBackground or whatever to be revealed in a future beta but I'd be interested to see what the correct solution is.

I have found a way to set the system background color using SwiftUI. It is pretty handy when you want the background color in dark mode of the modal view.

.foregroundColor(Color(UIColor.systemBackground))