<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:media="http://search.yahoo.com/mrss/"

	>
<channel>
	<title>
	Comments on: Hiding SwiftUI views	</title>
	<atom:link href="https://wadetregaskis.com/hiding-swiftui-views/feed/" rel="self" type="application/rss+xml" />
	<link>https://wadetregaskis.com/hiding-swiftui-views/</link>
	<description></description>
	<lastBuildDate>Wed, 04 Dec 2024 14:30:02 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>
		By: Anonymous		</title>
		<link>https://wadetregaskis.com/hiding-swiftui-views/#comment-4554</link>

		<dc:creator><![CDATA[Anonymous]]></dc:creator>
		<pubDate>Wed, 04 Dec 2024 14:30:02 +0000</pubDate>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7711#comment-4554</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://wadetregaskis.com/hiding-swiftui-views/#comment-3474&quot;&gt;David Whetstone&lt;/a&gt;.

Use .hidden() when you want to use a view only for layout purposes - never shown nor interacted with. It doesn&#039;t respond to custom animations for example.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://wadetregaskis.com/hiding-swiftui-views/#comment-3474" data-wpel-link="internal">David Whetstone</a>.</p>
<p>Use .hidden() when you want to use a view only for layout purposes &#8211; never shown nor interacted with. It doesn&#8217;t respond to custom animations for example.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ole Begemann		</title>
		<link>https://wadetregaskis.com/hiding-swiftui-views/#comment-3479</link>

		<dc:creator><![CDATA[Ole Begemann]]></dc:creator>
		<pubDate>Sat, 17 Feb 2024 19:32:29 +0000</pubDate>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7711#comment-3479</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://wadetregaskis.com/hiding-swiftui-views/#comment-3473&quot;&gt;Wade Tregaskis&lt;/a&gt;.

Thanks! I like your changes.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://wadetregaskis.com/hiding-swiftui-views/#comment-3473" data-wpel-link="internal">Wade Tregaskis</a>.</p>
<p>Thanks! I like your changes.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Wade Tregaskis		</title>
		<link>https://wadetregaskis.com/hiding-swiftui-views/#comment-3475</link>

		<dc:creator><![CDATA[Wade Tregaskis]]></dc:creator>
		<pubDate>Sat, 17 Feb 2024 00:34:48 +0000</pubDate>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7711#comment-3475</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://wadetregaskis.com/hiding-swiftui-views/#comment-3474&quot;&gt;David Whetstone&lt;/a&gt;.

Yeah, it&#039;s a bit tangential to the point I was focused on - using &lt;code&gt;EmptyView&lt;/code&gt; - but I did spend a little time trying to figure out what the &lt;code&gt;hidden&lt;/code&gt; modifier actually does.  i.e. does it just do &lt;code&gt;opacity(0)&lt;/code&gt;?  The way Apple talks about it, it sounds like that&#039;s actually the case.  But they never actually state it unequivocally.  So I&#039;m not certain what the difference might be; thus why I didn&#039;t recommend one over the other.

Unfortunately, I can&#039;t find the implementation of the &lt;code&gt;hidden&lt;/code&gt; modifier.  It doesn&#039;t seem to be part of the SwiftUI framework - no symbol entry for it.  So maybe it&#039;s always inlined?  But I couldn&#039;t find a source definition of it either, in the Xcode SDKs.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://wadetregaskis.com/hiding-swiftui-views/#comment-3474" data-wpel-link="internal">David Whetstone</a>.</p>
<p>Yeah, it&#8217;s a bit tangential to the point I was focused on &#8211; using <code>EmptyView</code> &#8211; but I did spend a little time trying to figure out what the <code>hidden</code> modifier actually does.  i.e. does it just do <code>opacity(0)</code>?  The way Apple talks about it, it sounds like that&#8217;s actually the case.  But they never actually state it unequivocally.  So I&#8217;m not certain what the difference might be; thus why I didn&#8217;t recommend one over the other.</p>
<p>Unfortunately, I can&#8217;t find the implementation of the <code>hidden</code> modifier.  It doesn&#8217;t seem to be part of the SwiftUI framework &#8211; no symbol entry for it.  So maybe it&#8217;s always inlined?  But I couldn&#8217;t find a source definition of it either, in the Xcode SDKs.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: David Whetstone		</title>
		<link>https://wadetregaskis.com/hiding-swiftui-views/#comment-3474</link>

		<dc:creator><![CDATA[David Whetstone]]></dc:creator>
		<pubDate>Fri, 16 Feb 2024 23:46:13 +0000</pubDate>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7711#comment-3474</guid>

					<description><![CDATA[Nice writeup! Consider adding a comment about when to use opacity vs. hidden? For example, &lt;code&gt;opacity(shouldHide ? 0 : 1)&lt;/code&gt; can be used to conditionally hide a view, while you&#039;d need an if/else and recreate the view in each clause to conditionally hide with hidden. There may be other reasons I&#039;m not aware of.]]></description>
			<content:encoded><![CDATA[<p>Nice writeup! Consider adding a comment about when to use opacity vs. hidden? For example, <code>opacity(shouldHide ? 0 : 1)</code> can be used to conditionally hide a view, while you&#8217;d need an if/else and recreate the view in each clause to conditionally hide with hidden. There may be other reasons I&#8217;m not aware of.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Wade Tregaskis		</title>
		<link>https://wadetregaskis.com/hiding-swiftui-views/#comment-3473</link>

		<dc:creator><![CDATA[Wade Tregaskis]]></dc:creator>
		<pubDate>Fri, 16 Feb 2024 22:52:05 +0000</pubDate>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7711#comment-3473</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://wadetregaskis.com/hiding-swiftui-views/#comment-3471&quot;&gt;Ole Begemann&lt;/a&gt;.

Post updated.  I stopped short of providing a &quot;real&quot; example of where &lt;code&gt;AnyView(EmptyView())&lt;/code&gt; makes obvious sense, as the view body needs to be pretty non-trivial before you reach that point.  But, I did try to spell out fundamentally why you might end up in that situation.  Hopefully it all makes sense now.  Let me know if anything remains unclear.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://wadetregaskis.com/hiding-swiftui-views/#comment-3471" data-wpel-link="internal">Ole Begemann</a>.</p>
<p>Post updated.  I stopped short of providing a &#8220;real&#8221; example of where <code>AnyView(EmptyView())</code> makes obvious sense, as the view body needs to be pretty non-trivial before you reach that point.  But, I did try to spell out fundamentally why you might end up in that situation.  Hopefully it all makes sense now.  Let me know if anything remains unclear.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Wade Tregaskis		</title>
		<link>https://wadetregaskis.com/hiding-swiftui-views/#comment-3472</link>

		<dc:creator><![CDATA[Wade Tregaskis]]></dc:creator>
		<pubDate>Fri, 16 Feb 2024 21:51:45 +0000</pubDate>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7711#comment-3472</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://wadetregaskis.com/hiding-swiftui-views/#comment-3471&quot;&gt;Ole Begemann&lt;/a&gt;.

It&#039;s true, in the highly simplified example I showed here you don&#039;t need to use &lt;code&gt;AnyView&lt;/code&gt;.  I&#039;ll revise it a bit to better show a situation where you do.  Thanks for your feedback on this - it&#039;s an important point and I&#039;m glad to improve this post&#039;s clarity.

I&#039;m not sure of the exact mechanics, but you can opt-out of &quot;ViewBuilder&quot; mode, intentionally or not.  e.g. if you explicit return.  And I find that&#039;s sometimes unavoidable (in a pragmatic sense - you can always contrive some amount of conditional logic that will effectively mimic early returns, but it can get ugly and IMO isn&#039;t worth the trade-off merely to avoid &lt;code&gt;AnyView&lt;/code&gt; - especially given the lack of any apparent real downside to &lt;code&gt;AnyView&lt;/code&gt; in terms of performance.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://wadetregaskis.com/hiding-swiftui-views/#comment-3471" data-wpel-link="internal">Ole Begemann</a>.</p>
<p>It&#8217;s true, in the highly simplified example I showed here you don&#8217;t need to use <code>AnyView</code>.  I&#8217;ll revise it a bit to better show a situation where you do.  Thanks for your feedback on this &#8211; it&#8217;s an important point and I&#8217;m glad to improve this post&#8217;s clarity.</p>
<p>I&#8217;m not sure of the exact mechanics, but you can opt-out of &#8220;ViewBuilder&#8221; mode, intentionally or not.  e.g. if you explicit return.  And I find that&#8217;s sometimes unavoidable (in a pragmatic sense &#8211; you can always contrive some amount of conditional logic that will effectively mimic early returns, but it can get ugly and IMO isn&#8217;t worth the trade-off merely to avoid <code>AnyView</code> &#8211; especially given the lack of any apparent real downside to <code>AnyView</code> in terms of performance.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ole Begemann		</title>
		<link>https://wadetregaskis.com/hiding-swiftui-views/#comment-3471</link>

		<dc:creator><![CDATA[Ole Begemann]]></dc:creator>
		<pubDate>Fri, 16 Feb 2024 21:35:07 +0000</pubDate>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7711#comment-3471</guid>

					<description><![CDATA[&lt;blockquote&gt;Note that use of AnyView is required in order to accomodate the SwiftUI View protocol requirement that body always return the same type of view.&lt;/blockquote&gt;

This is generally not necessary. The body property is implicitly annotated with @ViewBuilder (through the View protocol). So the usual ViewBuilder rules apply, i.e. you don‘t have to use AnyView nor does your if let require an else branch.

Both of these variants work too:

&lt;code&gt;&#160;&#160;&#160;&#160;var body: some View {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if let model {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Text(&quot;Hello&quot;)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;} else {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;EmptyView()
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;}

&#160;&#160;&#160;&#160;var body: some View {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if let model {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Text(&quot;Hello&quot;)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;}&lt;/code&gt;

For your own properties or functions, you can also mark them @ViewBuilder to get the same result. Example:

&lt;code&gt;&#160;&#160;&#160;&#160;@ViewBuilder var myHelperView: some View {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if let model {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Text(&quot;Hello&quot;)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;}&lt;/code&gt;]]></description>
			<content:encoded><![CDATA[<blockquote><p>Note that use of AnyView is required in order to accomodate the SwiftUI View protocol requirement that body always return the same type of view.</p></blockquote>
<p>This is generally not necessary. The body property is implicitly annotated with @ViewBuilder (through the View protocol). So the usual ViewBuilder rules apply, i.e. you don‘t have to use AnyView nor does your if let require an else branch.</p>
<p>Both of these variants work too:</p>
<p><code>&nbsp;&nbsp;&nbsp;&nbsp;var body: some View {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if let model {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text("Hello")<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EmptyView()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;var body: some View {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if let model {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text("Hello")<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></p>
<p>For your own properties or functions, you can also mark them @ViewBuilder to get the same result. Example:</p>
<p><code>&nbsp;&nbsp;&nbsp;&nbsp;@ViewBuilder var myHelperView: some View {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if let model {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text("Hello")<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
