<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Benoît Caron</title>
    <description>The latest articles on DEV Community by Benoît Caron (@benoitcaron).</description>
    <link>https://dev.to/benoitcaron</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F364055%2Ffbc55c80-47d6-45d2-ac79-ce9aa9906e79.jpeg</url>
      <title>DEV Community: Benoît Caron</title>
      <link>https://dev.to/benoitcaron</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benoitcaron"/>
    <language>en</language>
    <item>
      <title>Menus in a Catalyst app</title>
      <dc:creator>Benoît Caron</dc:creator>
      <pubDate>Fri, 10 Apr 2020 15:02:21 +0000</pubDate>
      <link>https://dev.to/benoitcaron/menus-in-a-catalyst-app-1ado</link>
      <guid>https://dev.to/benoitcaron/menus-in-a-catalyst-app-1ado</guid>
      <description>&lt;p&gt;So... Catalyst, huh? The ultimate promise that a click on a checkbox and minimal effort will bring your iPad app to the Mac. If you have ever tried porting an app with this technology, you surely know by now that it is unfortunately not that simple. But, hey, in the end you finally managed to port your app, in all its glory, to the Mac. You've got all, or most of your features, you revamped some controllers, you have it all under control. Catalyst is great, long live Catalyst.&lt;/p&gt;

&lt;p&gt;There is one thing that Catalyst won't do for you, though: menus. In order for your Catalyst app to feel right at home on the Mac, a well thought-out menu can really add to the experience. And, oddly enough, there are not that many resources on the subject. There are some, of course, but they really just scratch the surface. That article will not be the universal answer on the matter, but my hope is that it will help you a little bit more than what is currently out there. With that in mind, let's get started, shall we?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Menu Bar
&lt;/h2&gt;

&lt;p&gt;Throughout the numerous iterations of Mac OS (or macOS as of 2020), the Menu Bar has seen slight graphical evolutions, but at its core, its mission always remained the same: to provide easy access to features of your application. Often, the Menu Bar is actually the only way to even access those features!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gOMn9pGY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uignyuzvp1cbvtbj840j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gOMn9pGY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uignyuzvp1cbvtbj840j.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Mac OS 9.1 (Fortissimo) - 1999&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_qVOs6kC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/psalg1cr6k0hh44gqw0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_qVOs6kC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/psalg1cr6k0hh44gqw0b.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Mac OS X 10.4 (Tiger) - 2005&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Sl7PhXUf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mqjmxrfhce76ku00yezz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Sl7PhXUf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mqjmxrfhce76ku00yezz.png" alt="Alt Text"&gt;&lt;/a&gt; &lt;br&gt;
&lt;em&gt;macOS 10.15 (Catalina) – 2019&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, a little terminology. In this article, we will mainly make use of three &lt;code&gt;UIKit&lt;/code&gt; classes: &lt;code&gt;UIMenu&lt;/code&gt;, &lt;code&gt;UIKeyCommand&lt;/code&gt;, and &lt;code&gt;UICommand&lt;/code&gt;. Although they all inherit from the same &lt;code&gt;UIMenuElement&lt;/code&gt; class, they don’t have the same role:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;UIMenu&lt;/code&gt; is a container. Its job is to contain and regroup commands or other menus, called &lt;strong&gt;children&lt;/strong&gt;. From now on, the terms &lt;strong&gt;menu&lt;/strong&gt; and &lt;strong&gt;submenu&lt;/strong&gt; will refer to an instance of &lt;code&gt;UIMenu&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UIKeyCommand&lt;/code&gt; and &lt;code&gt;UICommand&lt;/code&gt; items are found within a menu. The main difference between a &lt;code&gt;UIKeyCommand&lt;/code&gt; and a &lt;code&gt;UICommand&lt;/code&gt; is the former’s ability to be associated with a keyboard shortcut. That said, they will perform the same, and as such they will simply be referred to as &lt;strong&gt;commands&lt;/strong&gt;, when there is no need to differentiate. &lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Menu composition
&lt;/h2&gt;

&lt;p&gt;Let’s take a look at the &lt;strong&gt;Edit menu&lt;/strong&gt; from the TextEdit app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4S0gjwSW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/83urlsi8re92zqzmjhl3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4S0gjwSW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/83urlsi8re92zqzmjhl3.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;TextEdit's Edit menu&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is quite a lot of stuff in there. Let’s deconstruct this menu so we have a better idea of how it was made.&lt;/p&gt;

&lt;p&gt;First, we have an &lt;strong&gt;Edit menu item&lt;/strong&gt; in the Menu Bar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LpRIqeFa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1oihlceuo5cdlekeczav.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LpRIqeFa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1oihlceuo5cdlekeczav.png" alt=""&gt;&lt;/a&gt; &lt;br&gt;
&lt;em&gt;Edit menu main structure&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As previously stated, this &lt;strong&gt;Edit menu&lt;/strong&gt; is merely a &lt;strong&gt;container&lt;/strong&gt;. It only contains what appears to be commands and submenus. &lt;/p&gt;

&lt;p&gt;If we delve a bit deeper, we can see that these commands are actually all contained within submenus. That’s right, each group of commands you see, separated by dividers, is actually &lt;strong&gt;another menu within the menu&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l0jg1q8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/geibn88qw6rvfau6egc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l0jg1q8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/geibn88qw6rvfau6egc6.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Edit menu detailed structure&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As we can see here, the &lt;strong&gt;Edit menu&lt;/strong&gt; is composed of five &lt;strong&gt;submenus&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first submenu contains two commands:

&lt;ul&gt;
&lt;li&gt;Both are &lt;code&gt;UIKeyCommands&lt;/code&gt;, shown by the keyboard shortcuts&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The second submenu contains seven commands:

&lt;ul&gt;
&lt;li&gt;One of them is a &lt;code&gt;UICommand&lt;/code&gt;, as there is no keyboard shortcut associated with it&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The third one is interesting:

&lt;ul&gt;
&lt;li&gt;Its children are a mixture of one menu and two commands&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The fourth one contains menus only&lt;/li&gt;
&lt;li&gt;And the fifth one contains commands only&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we go even deeper, we can observe that the &lt;strong&gt;Insert submenu&lt;/strong&gt; contains three commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rwS4xH75--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ihpf8jsurfbxv92mjhu4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rwS4xH75--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ihpf8jsurfbxv92mjhu4.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Insert menu main structure&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;By now, you may be wondering: how come some submenus, such as the &lt;strong&gt;Undo Redo menu&lt;/strong&gt;, display their commands as is, while others, such as the &lt;strong&gt;Insert menu&lt;/strong&gt;, group their commands one level deeper?&lt;/p&gt;

&lt;p&gt;This is all by design. A menu can be displayed either &lt;em&gt;inline&lt;/em&gt; (&lt;strong&gt;Undo Redo&lt;/strong&gt;), or as &lt;em&gt;its own entity&lt;/em&gt; (&lt;strong&gt;Insert&lt;/strong&gt;). This behavior is set by a property when creating the menu.&lt;/p&gt;

&lt;p&gt;Now that we have a good idea of what is a menu and how commands and submenus fit into it, let’s see how to make our own!&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a menu in Storyboard – easy, but limited
&lt;/h2&gt;

&lt;p&gt;Using the main storyboard of our app is the fastest and easiest way to have a simple menu up and running.&lt;/p&gt;

&lt;p&gt;The first thing we need to do is to drag a &lt;code&gt;Main Menu&lt;/code&gt; component in our storyboard. This will result in a standard Menu Bar showing alongside our controllers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u8Xo1b9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/py2y29krm1clwxlfvgo8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u8Xo1b9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/py2y29krm1clwxlfvgo8.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Main menu componant in component picker&lt;/em&gt;   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zte2Kmhu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ne9v1pfuqh1rq0tybrnr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zte2Kmhu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ne9v1pfuqh1rq0tybrnr.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Main menu componant in Interface Builder&lt;/em&gt;    &lt;/p&gt;

&lt;p&gt;From there, we can add any &lt;code&gt;Menu Command&lt;/code&gt;, &lt;code&gt;Sub Menu&lt;/code&gt; and / or &lt;code&gt;Inline Section Menu&lt;/code&gt;, until we get the Menu Bar that fits our needs.&lt;/p&gt;

&lt;p&gt;In this example, I’ve added a new &lt;code&gt;Sub Menu&lt;/code&gt;, conveniently named &lt;strong&gt;My New Menu&lt;/strong&gt;, as well as two &lt;code&gt;Menu Commands&lt;/code&gt;: &lt;strong&gt;First Command&lt;/strong&gt; and &lt;strong&gt;Second Command&lt;/strong&gt;. To rename them, we can double-click the current values and replace them, or use the &lt;strong&gt;Inspector&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9L0kLbYg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kevhbv8hs68lww3q9i2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9L0kLbYg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kevhbv8hs68lww3q9i2r.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Our first menu&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rm6mV6pW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2hq96fg3xowfy0pdhft2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rm6mV6pW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2hq96fg3xowfy0pdhft2.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Attributes inspector for a menu&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h0SM68j1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4xvpk81cqnt3b7u8tdu0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h0SM68j1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4xvpk81cqnt3b7u8tdu0.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Attributes inspector for a command&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The next step would be, naturally, to have actions linked to those commands. Those exist through &lt;code&gt;@IBAction&lt;/code&gt;s. In that way, it's not very different from a &lt;code&gt;UIButton&lt;/code&gt;’s behaviour.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;firstCommandAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;secondCommandAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Connecting those &lt;code&gt;@IBAction&lt;/code&gt;s to our commands is really simple, as it usually is with Interface Builder: we right-click a command and drag the connecting line to the &lt;strong&gt;First Responder&lt;/strong&gt;, just above it. Our actions will be presented among others (and there might be quite a lot of them here).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cW6Zgm8t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9bc68sed9j74cwyssk41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cW6Zgm8t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9bc68sed9j74cwyssk41.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Linking our menu to the first responder&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s862-2Jp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eoy3rzjimvlmm2va24e8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s862-2Jp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eoy3rzjimvlmm2va24e8.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Selecting the action to link against&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ If our command is not connected to any action, &lt;strong&gt;it will stay greyed out&lt;/strong&gt; when our app is launched.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, we will add &lt;strong&gt;keyboard shortcuts&lt;/strong&gt; to those commands. This is easily done by double-clicking on the area at the right of the command’s title, or in the Inspector.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1JWIj6rf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sysnsgnxrgt3zdm0z1i6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1JWIj6rf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sysnsgnxrgt3zdm0z1i6.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Selecting the keyboard shortcut area&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W4LQV7EX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gcmbkv1otkbbfbfjc0bw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W4LQV7EX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gcmbkv1otkbbfbfjc0bw.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Setting the keyboard shortcut&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Please note that &lt;strong&gt;our app will crash&lt;/strong&gt; if we try to “steal” an already attributed keyboard shortcut, such as ⌘C&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we take a closer look at the Inspector, we find a bunch of options, which differ for menus and commands.&lt;/p&gt;

&lt;p&gt;I won’t go into much detail here, as those will be covered in the next chapter.&lt;/p&gt;

&lt;p&gt;If we want to alter a command at runtime (change its name, check it, grey it out, etc.), we will need to override &lt;code&gt;UIResponder&lt;/code&gt;’s &lt;code&gt;validate(:_)&lt;/code&gt; method in the same class our &lt;code&gt;@IBAction&lt;/code&gt; is defined in. This will also be covered later.&lt;/p&gt;



&lt;p&gt;Well, that was easy! Still, this way of implementing menus and commands has a very serious limitation: &lt;strong&gt;it is static&lt;/strong&gt;. Once we have created our menus and commands, this is it. It cannot be changed at runtime.&lt;/p&gt;

&lt;p&gt;You might wonder why someone would want to change a list of menus and commands at runtime. It does not seem very obvious why they would want a dynamic list of menus, or commands. And yet, from your browser, right now (if you are on a computer browser), click on the &lt;strong&gt;History&lt;/strong&gt; menu. This menu does need to be dynamic. It needs to be able to show you, at any time, the latest websites you have visited. This cannot be predetermined. This is one of the reasons a dynamic menu might be needed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating menus in code - harder, (better?) &lt;del&gt;faster&lt;/del&gt;, stronger
&lt;/h2&gt;

&lt;p&gt;Creating menus and commands programmatically is the alternative to creating them in storyboards (who knew!). This gives us much more control, but is also more difficult. That can lead to a lot of trial and error, since there are many things that can prevent our menus from working as expected.&lt;/p&gt;

&lt;p&gt;Our entry point will be &lt;code&gt;UIReponder&lt;/code&gt;’s &lt;code&gt;buildMenu(with:)&lt;/code&gt; method. It will provide us with the &lt;code&gt;UIMenuBuilder&lt;/code&gt; needed to build our menu.&lt;/p&gt;

&lt;p&gt;First things first, let’s override this method in our &lt;strong&gt;AppDelagate&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;buildMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="nv"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenuBuilder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This article focuses on the &lt;strong&gt;Menu Bar&lt;/strong&gt;, also known as the &lt;strong&gt;main system menu&lt;/strong&gt;, so we will make sure to only use the main menu’s builder (as opposed to contextual menus).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;system&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From here, the builder object is what will allow us to manipulate the &lt;strong&gt;Menu Bar&lt;/strong&gt;, remove existing menus, add new ones, etc.&lt;/p&gt;

&lt;p&gt;For more information on the &lt;code&gt;UIMenuBuilder&lt;/code&gt; protocol, please have a look at the &lt;a href="https://developer.apple.com/documentation/uikit/uimenubuilder"&gt;Apple documentation.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will focus our attention on a limited number of the methods exposed by this class, as this is enough to get a good grasp of its usage.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;remove(menu:)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This one is pretty straightforward. The default &lt;strong&gt;Menu Bar&lt;/strong&gt; provided with every application surely contains menus which are useless to us.&lt;/p&gt;

&lt;p&gt;Each menu is usually associated with a &lt;strong&gt;unique identifier&lt;/strong&gt;, which will be used as a parameter to tell Xcode precisely which menu to remove.&lt;/p&gt;

&lt;p&gt;Remember our &lt;strong&gt;Edit menu&lt;/strong&gt;, which is itself composed of several other &lt;strong&gt;submenus&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l0jg1q8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/geibn88qw6rvfau6egc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l0jg1q8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/geibn88qw6rvfau6egc6.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Edit menu detailed structure&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can remove what we called &lt;strong&gt;submenu 1&lt;/strong&gt; on this capture, leaving all the other menus intact, thanks to this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;menu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;undoRedo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wWK42_kP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bz0qidmmdhnwkqng6sa1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wWK42_kP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bz0qidmmdhnwkqng6sa1.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The Undo Redo submenu is removed&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can also remove the whole &lt;strong&gt;Edit menu&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;menu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;edit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NrtYyTQA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nd5pk6a09sru4gd9ijwp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NrtYyTQA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nd5pk6a09sru4gd9ijwp.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The Edit menu is removed&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ This method cannot be used to remove individual commands.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;insertSibling(_:beforeMenu:)&lt;/code&gt; | &lt;code&gt;insertSibling(_:afterMenu:)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If used with a &lt;strong&gt;top-level menu&lt;/strong&gt; (such as the &lt;strong&gt;Edit menu&lt;/strong&gt; in this example), these methods will allow us to create new top-level menus.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;menu1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Menu 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertSibling&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;menu1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="nv"&gt;afterMenu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;edit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TlMYLofm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u2d2qetbd670w9qgt3ox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TlMYLofm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u2d2qetbd670w9qgt3ox.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The new Menu 1 menu has been inserted as a top-level sibling&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If used with a &lt;strong&gt;submenu&lt;/strong&gt; (such as the &lt;strong&gt;Undo Redo submenu&lt;/strong&gt; in this example), this will create a new submenu entry, &lt;strong&gt;with dividers&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;menu1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Menu 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertSibling&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;menu1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="nv"&gt;afterMenu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;undoRedo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UmxRYOAK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mvrksodvcr191h55dn87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UmxRYOAK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mvrksodvcr191h55dn87.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The new Menu 1 menu has been inserted as a submenu sibling&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;insertChild(_:atStartOfMenu:)&lt;/code&gt; | &lt;code&gt;insertChild(_:atEndOfMenu:)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If &lt;code&gt;insertChild(_:atStartOfMenu:)&lt;/code&gt; is used with a &lt;strong&gt;top-level menu&lt;/strong&gt;, it will have the same effect as using &lt;code&gt;insertSibling(_:beforeMenu:)&lt;/code&gt; with the first submenu.&lt;/p&gt;

&lt;p&gt;If used with a &lt;strong&gt;submenu&lt;/strong&gt;, this will add our menu as if it was part of that submenu, &lt;strong&gt;without any divider&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;menu1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Menu 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;menu1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nv"&gt;atEndOfMenu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;undoRedo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z5JT8HMy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zgp8g3pjx6607rdmunh5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z5JT8HMy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zgp8g3pjx6607rdmunh5.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The new Menu 1 menu has been inserted as a submenu child&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, as the name suggests, &lt;code&gt;insertSibling&lt;/code&gt; will add a new menu with the &lt;strong&gt;same hierarchy level&lt;/strong&gt; as the reference menu.&lt;br&gt;
&lt;code&gt;insertChild&lt;/code&gt;, on the other hand, will add a new menu with a &lt;strong&gt;lower hierarchy level&lt;/strong&gt; than the reference menu.&lt;/p&gt;
&lt;h3&gt;
  
  
  Instantiating a UIMenu
&lt;/h3&gt;

&lt;p&gt;As seen in the previous examples, the &lt;code&gt;UIMenu&lt;/code&gt; class is used to create the object that represents our menu.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nv"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
       &lt;span class="nv"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Identifier&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
       &lt;span class="nv"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nv"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UIMenuElement&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let’s have a look at all those properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;title&lt;/code&gt;: the title of our menu&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;image&lt;/code&gt;: an image to be set to the left of the menu’s title (15x15 ideally, but can be bigger)

&lt;ul&gt;
&lt;li&gt;In my testing, this parameter had no effect whatsoever. I reckon this feature is broken in the current version of Catalyst&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;identifier&lt;/code&gt;: a unique identifier for our menu. While this parameter is not mandatory, it will be essential if / when you need to reference the menu when inserting siblings or children. It can be ignored if you don’t intend to reference it ever

&lt;ul&gt;
&lt;li&gt;If this parameter is set, it must be &lt;strong&gt;absolutely unique&lt;/strong&gt; within your application. Failing to meet this requirement will result in a menu no showing&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;options&lt;/code&gt;: defines how our menu appears (option set)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[]&lt;/code&gt; will either be used for a top-level menu, as well as a dropdown submenu, as shown in the example above (&lt;strong&gt;Menu 1 menu&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.displayInline&lt;/code&gt; will hide the menu title and directly expose its children, as shown in the example above (&lt;strong&gt;Undo Redo menu&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.destructive&lt;/code&gt; will change the menu’s appearance to represent a destructive action

&lt;ul&gt;
&lt;li&gt;In my testing, this value had no effect whatsoever. I reckon this feature is broken in the current version of Catalyst&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;children&lt;/code&gt;: the list of commands and / or menus to be displayed by the menu we are defining&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of these parameters have default values, so we don’t need to define them if we don’t use them.&lt;br&gt;
For example, the image parameter being rarely used (or never as long as it doesn’t work), we can use&lt;br&gt;
:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nv"&gt;identifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Identifier&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
       &lt;span class="nv"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nv"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UIMenuElement&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or even&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;UIMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nv"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UIMenuElement&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For more information on the &lt;code&gt;UIMenu&lt;/code&gt; class, please have a look at the &lt;a href="https://developer.apple.com/documentation/uikit/uimenu"&gt;Apple documentation.&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Tips and watch points&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As stated before, a menu is merely a container for a list of commands and / or submenus. That said, if you intend to create a top-level menu, &lt;strong&gt;I would advise&lt;/strong&gt; not to directly add commands as children, but rather create inline submenus for those commands. This will make it easier later on if you ever need to add more children or siblings, as you will have a reference to those submenus&lt;/li&gt;
&lt;li&gt;An identifier, if defined, &lt;strong&gt;has to be unique&lt;/strong&gt;. If this requirement is not met, the menu will not display&lt;/li&gt;
&lt;li&gt;If your menu is displayed inline, the title value will be unused&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Instantiating a command
&lt;/h3&gt;

&lt;p&gt;As we have said before, there will be two kinds of commands for us to study: &lt;code&gt;UIKeyCommand&lt;/code&gt; and &lt;code&gt;UICommand&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  With a keyboard shortcut
&lt;/h4&gt;

&lt;p&gt;Let’s start with the former.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;UIKeyCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
             &lt;span class="nv"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Selector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;modifierFlags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIKeyModifierFlags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;propertyList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
             &lt;span class="nv"&gt;alternates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UICommandAlternate&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
             &lt;span class="nv"&gt;discoverabilityTitle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
             &lt;span class="nv"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenuElement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Attributes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenuElement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As for &lt;code&gt;UIMenu&lt;/code&gt;, let’s explain what we have here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;title&lt;/code&gt;: the title of our command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;image&lt;/code&gt;: an image to be set to the left of the command’s title (15x15 ideally, but can be bigger)

&lt;ul&gt;
&lt;li&gt;Unlike &lt;code&gt;UIMenu&lt;/code&gt;’s image, this one works as intended. You are not limited in the size of the image, but any image larger than 15x15 will change the command’s height accordingly&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;action&lt;/code&gt;: represents the method that should be called upon selection

&lt;ul&gt;
&lt;li&gt;This method needs to accept parameters if several commands call the same action&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;input&lt;/code&gt;: a string representing the keyboard key defining the keyboard shorcut

&lt;ul&gt;
&lt;li&gt;Some values exist for special keys (including, but not limited to: arrow keys, escape key, function keys, etc.). These may be used as such: &lt;code&gt;UIKeyCommand.inputDownArrow&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;modifierFlags&lt;/code&gt;: the special keys that will need to be pressed for the command to execute (option set)

&lt;ul&gt;
&lt;li&gt;Including, but not limited to: command, alternate, control, shift, etc.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;propertyList&lt;/code&gt;: an object that contains data to associate with the key command

&lt;ul&gt;
&lt;li&gt;This is useful when several commands use the same selector, to establish the one from which the call originated from&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;alternates&lt;/code&gt;: defines alternate behaviours (option set)

&lt;ul&gt;
&lt;li&gt;The same input will be used, but different modifier flags, title and selector will be defined&lt;/li&gt;
&lt;li&gt;An example can be found in the &lt;strong&gt;Apple Menu&lt;/strong&gt;: &lt;strong&gt;About this Mac&lt;/strong&gt; is turned into &lt;strong&gt;System Information…&lt;/strong&gt; when holding the option key&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;discoverabilityTitle&lt;/code&gt;: not used on Mac

&lt;ul&gt;
&lt;li&gt;This is actually used on iPad to display a string in a cheat sheet in the middle of the screen, when holding down the command key&lt;/li&gt;
&lt;li&gt;A really nice article &lt;a href="https://useyourloaf.com/blog/adding-hardware-keyboard-shortcuts/"&gt;can be found here&lt;/a&gt; if you are interested in the subject&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Attributes&lt;/code&gt;: defines whether a command is &lt;strong&gt;disabled&lt;/strong&gt;, &lt;strong&gt;destructive&lt;/strong&gt;, &lt;strong&gt;hidden&lt;/strong&gt;, or any &lt;strong&gt;combination&lt;/strong&gt; of the above (option set)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.disabled&lt;/code&gt; will grey the command out&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.hidden&lt;/code&gt; will hide the command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.destructive&lt;/code&gt; will display the command in a more prominent style, such as with red text

&lt;ul&gt;
&lt;li&gt;In my testing, this value had no effect whatsoever. I reckon this feature is broken in the current version of Catalyst.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;State&lt;/code&gt;: adds a symbol to the left of the command’s title (enum)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.off&lt;/code&gt; displays nothing&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.mixed&lt;/code&gt; displays a hyphen&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.on&lt;/code&gt; displays a checkmark&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As for &lt;code&gt;UIMenu&lt;/code&gt;, most of these parameters have a default value, so we don’t need all of them every time we want to create a command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;UIKeyCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Selector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nv"&gt;modifierFlags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIKeyModifierFlags&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;is perfectly acceptable.&lt;/p&gt;

&lt;p&gt;For more information on the &lt;code&gt;UIKeyCommand&lt;/code&gt; class, please have a look at the &lt;a href="https://developer.apple.com/documentation/uikit/uikeycommand"&gt;Apple documentation.&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Without a keyboard shortcut
&lt;/h4&gt;

&lt;p&gt;If we don’t intend to use any input and modifier flags, we will need to use &lt;code&gt;UICommand&lt;/code&gt; instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;UICommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nv"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
          &lt;span class="nv"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Selector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nv"&gt;propertyList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
          &lt;span class="nv"&gt;alternates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UICommandAlternate&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
          &lt;span class="nv"&gt;discoverabilityTitle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
          &lt;span class="nv"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenuElement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Attributes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nv"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIMenuElement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Except for the previously stated &lt;code&gt;input&lt;/code&gt; and &lt;code&gt;modifierFlags&lt;/code&gt;, all parameters are exactly the same as for &lt;code&gt;UIKeyCommand&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Tips and watch points&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each command must be &lt;strong&gt;absolutely unique&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If we reuse the same input for several commands, &lt;strong&gt;our app will crash&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If we use the same selector for several commands, we must make absolutely sure our property list’s content &lt;strong&gt;is different for each command&lt;/strong&gt;, otherwise our app will &lt;strong&gt;either crash or won’t show our commands&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;propertyList&lt;/code&gt; can only contain plist-safe types (&lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;Date&lt;/code&gt;, &lt;code&gt;Data&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, and &lt;code&gt;Dictionary&lt;/code&gt;). Other types &lt;strong&gt;might lead to a crash&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;In the sample code I will distribute below, all of the selector’s methods are defined in the &lt;strong&gt;AppDelegate&lt;/strong&gt;. These methods can be defined in other classes, but I encountered some issues while testing. I would advise to keep all of those methods in the AppDelegate to guarantee success, but feel free to experiment.&lt;/li&gt;
&lt;li&gt;If your attributes and / or state properties are subject to change as the user uses your app, don’t bother giving them an initial value &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;.hidden&lt;/code&gt; attribute is not enough to prevent a command from being used – it can still be used if &lt;code&gt;input&lt;/code&gt; and &lt;code&gt;modifierFlags&lt;/code&gt; are defined. If you want to hide a command &lt;strong&gt;and&lt;/strong&gt; prevent it from being used, you should also use the &lt;code&gt;.disabled&lt;/code&gt; attribute&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Again, it may sound redundant, but &lt;strong&gt;unicity&lt;/strong&gt; is the key here. If there is an issue with a command not showing, there is a great probability that &lt;strong&gt;unicity is the issue&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is what instantiating a command might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIKeyCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Search"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;#selector(&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="kd"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"F"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;modifierFlags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;@objc&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or, with a selector shared between multiple commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIKeyCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Open &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;segmentedControl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;titleForSegment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;#selector(&lt;/span&gt;&lt;span class="nf"&gt;open(_:)&lt;/span&gt;&lt;span class="kd"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;modifierFlags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;propertyList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"segmentIndex"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="kd"&gt;@objc&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AnyObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="kt"&gt;UICommand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;propertyList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;propertyList&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;segmentIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;propertyList&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"segmentIndex"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Updating commands with &lt;code&gt;validate(:_)&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we have the tools to build menus, submenus, and commands, let's see how we can update them.&lt;/p&gt;

&lt;p&gt;At times, we might need to disable a command. On some instances, we might need to check, or uncheck a command, or even change its title or image. This is accomplished with the &lt;code&gt;UIResponder&lt;/code&gt;’s &lt;code&gt;validate(:_)&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;validate(:_)&lt;/code&gt; method is called whenever a command is about to be shown on screen.&lt;/p&gt;

&lt;p&gt;Based on the two commands we created before, here is what it would look like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;command&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UICommand&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kd"&gt;#selector(&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="kd"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kt"&gt;LoginViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disabled&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is where we should determine whether our command must be enabled, checked, hidden, etc.&lt;/p&gt;

&lt;p&gt;For more information on the &lt;code&gt;validate(:_)&lt;/code&gt; method, please have a look at the &lt;a href="https://developer.apple.com/documentation/uikit/uiresponder/3229892-validate"&gt;Apple documentation.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing a menu’s composition contextually
&lt;/h2&gt;

&lt;p&gt;We do have the possibility to change a menu’s composition after our app has been launched, if needed. We addressed such a case earlier with the browser’s &lt;strong&gt;History menu&lt;/strong&gt; example.&lt;/p&gt;

&lt;p&gt;Whenever we need our menu to be rebuilt, we can just call &lt;code&gt;UIMenuSystem.main.setNeedsRebuild()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This will trigger the &lt;code&gt;buildMenu(with:)&lt;/code&gt; method to be called again. Then, whatever condition we may have set to allow (or not) the creation of menus and commands will be evaluated again and allow us to have the menu we need at any time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sample code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/uikit/uicommand/adding_menus_and_shortcuts_to_the_menu_bar_and_user_interface"&gt;Apple&lt;/a&gt; has a sample code available. It does not cover as much as this article does, but I did use it as a reference. &lt;a href="https://github.com/BenoitCaron/Catalyst-Menus"&gt;My own&lt;/a&gt; sample code is available on Github, and covers everything I wrote about, save for the Storyboard section.&lt;/p&gt;

&lt;p&gt;Feel free to ask any question that you may still have.&lt;/p&gt;

</description>
      <category>catalyst</category>
      <category>swift</category>
      <category>ios</category>
    </item>
  </channel>
</rss>
