<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Paul's Blog]]></title><description><![CDATA[Paul's Blog]]></description><link>https://blog.tavitian.net.au</link><generator>RSS for Node</generator><lastBuildDate>Sat, 23 May 2026 15:24:08 GMT</lastBuildDate><atom:link href="https://blog.tavitian.net.au/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Detecting dark mode in SwiftUI]]></title><description><![CDATA[SwiftUI lets us detect whether dark mode or light mode is currently enabled using the colorScheme environment key. If you declare this using @Environment, you can refer to it in your views and they will automatically be reloaded when the colour schem...]]></description><link>https://blog.tavitian.net.au/detecting-dark-mode-in-swiftui</link><guid isPermaLink="true">https://blog.tavitian.net.au/detecting-dark-mode-in-swiftui</guid><dc:creator><![CDATA[Paul Tavitian]]></dc:creator><pubDate>Fri, 09 Apr 2021 07:23:32 GMT</pubDate><content:encoded><![CDATA[<p>SwiftUI lets us detect whether dark mode or light mode is currently enabled using the <code>colorScheme</code> environment key. If you declare this using <code>@Environment</code>, you can refer to it in your views and they will automatically be reloaded when the colour scheme changes.</p>
<p>For example, this will print either “In dark mode” or “In light mode” depending on the current colour scheme:</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ContentView</span>: <span class="hljs-title">View</span> </span>{
    @<span class="hljs-type">Environment</span>(\.colorScheme) <span class="hljs-keyword">var</span> colorScheme

    <span class="hljs-keyword">var</span> body: some <span class="hljs-type">View</span> {
        <span class="hljs-type">Text</span>(colorScheme == .dark ? <span class="hljs-string">"In dark mode"</span> : <span class="hljs-string">"In light mode"</span>)
    }
}
</code></pre>
<p>This should generally only be necessary for custom drawing – if you want to enable dark and light colours or dark and light images these can both be done using your asset catalog, and will also reload when the colour scheme changes.</p>
]]></content:encoded></item></channel></rss>