SwiftUI offers theme dependent Color
s like .primary
and .secondary
. How to get inverted versions of these (to use as background for example)? System itself knows which background color to use depending on light/dark theme.
1 Answer
you can use the colors "label" and "systembackground", they are usually opposite.
So use UIColor.systemBackground and UIColor.systemLabel for the opposite
or UIColor.secondarySystemBackground and UIColor.secondarySystemLabel etc...
-
-
3I was looking for SwiftUI way of doing this, not UIKit way. But since we can do
Color(uiColor: UIColor.systemBackground)
in iOS 15 to get theColor
bridged toUIColor
the suggested solution is a working one, @Abv. There are plenty of theme dependent colors in UIColor.– Paul BCommented Oct 14, 2021 at 15:35 -
UIColor
with neededColor
then invert it (using approach like in stackoverflow.com/a/5901586/12299030) then convert back by creatingColor
with resultUIColor
..colorInvert()
on the color. It takes the current color, adjusted for dark mode, etc, and returns its inverse..colorInvert()
producessome View
and cannot be used whereColor
parameter is expected, @Yrb. Though it is possible for example to use it on aText
to invert its color of course.