<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>SwiftUI on Mike Gopsill</title>
    <link>https://www.mikegopsill.com/categories/swiftui/</link>
    <description>Recent content in SwiftUI on Mike Gopsill</description>
    <generator>Hugo -- 0.161.1</generator>
    <language>en</language>
    <lastBuildDate>Thu, 19 Jun 2025 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://www.mikegopsill.com/categories/swiftui/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>SwiftUI Stacks (HStack, VStack, ZStack)</title>
      <link>https://www.mikegopsill.com/posts/swiftui-stacks-hstack-vstack/</link>
      <pubDate>Thu, 19 Jun 2025 00:00:00 +0000</pubDate>
      <guid>https://www.mikegopsill.com/posts/swiftui-stacks-hstack-vstack/</guid>
      <description>&lt;p&gt;SwiftUI provides three basic containers for arranging views: &lt;code&gt;HStack&lt;/code&gt;, &lt;code&gt;VStack&lt;/code&gt;, and &lt;code&gt;ZStack&lt;/code&gt;. These stacks are used for creating simple and complex user interfaces. Let&amp;rsquo;s see how they work.&lt;/p&gt;
&lt;h2 id=&#34;vstack-arranging-views-vertically&#34;&gt;VStack: Arranging Views Vertically&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;VStack&lt;/code&gt; (Vertical Stack) arranges its child views in a vertical line, one on top of the other.&lt;/p&gt;
&lt;h3 id=&#34;example&#34;&gt;Example:&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;VStack&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;Text&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Top Item&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;Text&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Middle Item&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;Text&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Bottom Item&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will display three text views stacked vertically.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;swiftui vstack&#34; loading=&#34;lazy&#34; src=&#34;https://www.mikegopsill.com/posts/swiftui-stacks-hstack-vstack/swiftui_vstack.png#center&#34;&gt;&lt;/p&gt;
&lt;p&gt;By default, views within a &lt;code&gt;VStack&lt;/code&gt; are centered horizontally. You can control the alignment using the &lt;code&gt;alignment&lt;/code&gt; parameter:&lt;/p&gt;</description>
    </item>
    <item>
      <title>SwiftUI Text View</title>
      <link>https://www.mikegopsill.com/posts/swiftui-basics-text-view/</link>
      <pubDate>Sun, 15 Jun 2025 00:00:00 +0000</pubDate>
      <guid>https://www.mikegopsill.com/posts/swiftui-basics-text-view/</guid>
      <description>&lt;p&gt;The role of &lt;code&gt;Text&lt;/code&gt; view in SwiftUI is to display text (funny that). It can support simple labels, rich content or dynamic text. It is the SwiftUI equivalent to &lt;code&gt;UILabel&lt;/code&gt;. Let&amp;rsquo;s get look at it in more detail.&lt;/p&gt;
&lt;h2 id=&#34;creating-basic-text&#34;&gt;Creating Basic Text&lt;/h2&gt;
&lt;p&gt;Simply pass a string to the &lt;code&gt;Text&lt;/code&gt; initialiser:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;Text&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Hello, SwiftUI!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img alt=&#34;swiftui text view&#34; loading=&#34;lazy&#34; src=&#34;https://www.mikegopsill.com/posts/swiftui-basics-text-view/swiftui_text.png#center&#34;&gt;&lt;/p&gt;
&lt;p&gt;You can also use string interpolation to create dynamic text:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;userName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Mike&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;Text&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Welcome back, &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;userName&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img alt=&#34;swiftui text view username&#34; loading=&#34;lazy&#34; src=&#34;https://www.mikegopsill.com/posts/swiftui-basics-text-view/swiftui_text_username.png#center&#34;&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Searchable Folders in SwiftUI</title>
      <link>https://www.mikegopsill.com/posts/custom-outlinegroup-search/</link>
      <pubDate>Sat, 31 May 2025 00:00:00 +0000</pubDate>
      <guid>https://www.mikegopsill.com/posts/custom-outlinegroup-search/</guid>
      <description>&lt;p&gt;If you haven’t met &lt;code&gt;OutlineGroup&lt;/code&gt; in SwiftUI yet, picture Finder’s sidebar rendered in SwiftUI: click a chevron, folder children appear. I recently tried to get this working with search. I wanted folders to open and close depending on whether the contained valid results&amp;hellip;&lt;/p&gt;
&lt;p&gt;After much wrestling and many coffees I gave up on &lt;code&gt;OutlineGroup&lt;/code&gt; in favour of a tiny &lt;code&gt;Node&lt;/code&gt; model, a &lt;code&gt;ExplorerStore&lt;/code&gt; viewmodel, and some recursion.&lt;/p&gt;
&lt;p&gt;The result? A searchable file‑browser that behaves at least as my mental model thinks it should.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Using Context Menu with Previews in SwiftUI</title>
      <link>https://www.mikegopsill.com/posts/swiftui-context-menu-previews/</link>
      <pubDate>Sun, 11 May 2025 08:00:00 +0000</pubDate>
      <guid>https://www.mikegopsill.com/posts/swiftui-context-menu-previews/</guid>
      <description>&lt;p&gt;SwiftUI’s &lt;code&gt;contextMenu&lt;/code&gt; modifier is a simple API that does a lot for us.&lt;/p&gt;
&lt;p&gt;I wrote about &lt;strong&gt;building context menus in both &lt;a href=&#34;https://www.mikegopsill.com/posts/uikit-context-menus/&#34;&gt;UIKit&lt;/a&gt; and &lt;a href=&#34;https://www.mikegopsill.com/posts/swiftui-context-menus/&#34;&gt;SwiftUI&lt;/a&gt;&lt;/strong&gt; recently. I did not go into detail about support for the &lt;strong&gt;custom previews&lt;/strong&gt; that landed in iOS 16, iPadOS 16 and macOS 13. Let us fill that gap.&lt;/p&gt;
&lt;h2 id=&#34;recap-context-menus-in-swiftui&#34;&gt;Recap: Context menus in SwiftUI&lt;/h2&gt;
&lt;p&gt;A context menu is equivalent of a right-click on touch devices. Using long‑press (or Control‑click on the Mac) on a view reveals a menu of options exactly where the user needs it. In pure SwiftUI we use &lt;code&gt;.contextMenu&lt;/code&gt; to create these menus:&lt;/p&gt;</description>
    </item>
    <item>
      <title>How to use Context Menus in SwiftUI</title>
      <link>https://www.mikegopsill.com/posts/swiftui-context-menus/</link>
      <pubDate>Thu, 08 May 2025 00:00:00 +0000</pubDate>
      <guid>https://www.mikegopsill.com/posts/swiftui-context-menus/</guid>
      <description>&lt;p&gt;The aim for this post to build a very simple &lt;code&gt;List&lt;/code&gt; view. When you long-press a row you will see a classic iOS context menu. It will look a little like this:&lt;/p&gt;
&lt;p&gt;&lt;img alt=&#34;Context-menu demo&#34; loading=&#34;lazy&#34; src=&#34;https://www.mikegopsill.com/posts/swiftui-context-menus/context-menu-demo.gif#center&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;why-context-menus&#34;&gt;Why Context Menus?&lt;/h2&gt;
&lt;p&gt;Context menus give users an easy way to discover secondary actions &lt;em&gt;right where they’re looking&lt;/em&gt;. They:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Keep the primary UI clean.&lt;/li&gt;
&lt;li&gt;Surface actions only when they’re relevant.&lt;/li&gt;
&lt;li&gt;Feel familiar thanks to the long-press gesture we already use across iOS.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;what-well-build&#34;&gt;What We’ll Build&lt;/h2&gt;
&lt;p&gt;We’ll create a simple list of fruit. A long-press on any row reveals three actions:&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
