简体   繁体   中英

Overwrite the default background color of SwiftUI Views?

In dark mode, the standard background color of View is pure black. While this does look nice on an iPhone X for example, it does cause “OLED smearing” when scrolling or doing other animations. Depending on the specific UI it can look really bad and obvious: scroll a list with the grouped list style where the cells have a different background color, and the cells seem to shrink while scrolling. This is really easily solved by using 050505 instead of pure black (see also https://medium.com/lookup-design/designing-a-dark-theme-for-oled-iphones-e13cdfea7ffe , andhttps://twitter.com/marcedwards/status/1053519077958803456?s=21 for a demonstration of what OLED smearing is).

Now, of course I could override the background color of every View in my app to use a custom color value. But that kinda sucks, is easy to forget, etc. So instead, how would one change the default color?

Edit: I know how to make custom colors, using the Asset Catalog or without, with different values for dark and light mode. That's not the problem. The question is specifically how to overwrite the default color.

I'm not certain if there is a way to override the default dark mode color, but you could configure a custom color set which has one color for light mode and then is a different color for dark mode. You would then just use this color set name throughout your app as you would any other color name. This doesn't solve your mentioning of needing to remember to use this color, but it may make it easier.

  1. In Assets.xcassets, click the + at the bottom and then "New Color Set"
  2. You can give your color a name
  3. In "Appearances" within the attributes inspector, select "Any, Dark"
  4. You can then set a different color for each and then use the name from Step 2 as your color in your app.

Example: I can use "BlackToWhite" as my color within my app, and anywhere I've done this, in light mode it will be black, and in dark mode it will be white

示例:我可以在我的应用程序中使用“BlackToWhite”作为我的颜色,在我做过的任何地方,在浅色模式下它是黑色,在深色模式下它是白色

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM