How to Show a Menu When a Button is Pressed in SwiftUI
Tags: swift, swiftui, ios
Published: 2020-09-02 17:38:34

---
title: How to Show a Menu When a Button is Pressed in SwiftUI
published: true
description: 
tags: #swift #SwiftUI #iOS
//cover_image: https://direct_url_to_image.jpg
---
<!-- wp:paragraph -->
<p>As of iOS 14 and SwiftUI 2 we can now add a pop out menu to any button in our app. We can implement this by using the new <strong><a href="https://developer.apple.com/documentation/swiftui/menu?changes=latest_beta">Menu</a></strong> keyword. Menus in some way are going to replace the current action sheets used in iOS apps. The problem with action sheets is they only show up at the bottom of our screen. This doesn't look great on larger screens like the iPad and Mac. Menus allow the flexibility in where we would want to show pop out menus in our app instead of only showing them at the bottom of our screens. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's take a look at the example below of how we can implement this in our application.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">struct ContentView: View {
    var body: some View {
        
        Menu("Create") {
            Button("Cancel", action: {})
            Button("Search", action: {})
            Button("Add", action: {})
        }
    }
}
</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>As you can see in the example above we use the <strong>Menu</strong> keyword and name our menu "<strong>Create</strong>". Next inside the menu curly braces we can add as many buttons as we want. The order of how we place the buttons in the menu does matter. The first button we create will always be at the bottom and whatever button we create next will be on top of the pervious the button. Take a look at the example below.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":921,"width":316,"height":614,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/09/screen-shot-2020-08-31-at-8.08.59-pm.png?w=527" alt="" class="wp-image-921" width="316" height="614"/></figure></div>
<!-- /wp:image -->

<!-- wp:image {"align":"center","id":923,"width":316,"height":614,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/09/screen-shot-2020-08-31-at-8.09.14-pm.png?w=519" alt="" class="wp-image-923" width="316" height="614"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>As you can see in the above example once our create button is tapped we can see our menu of buttons appear in the specific order we placed them in our code. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We can also have menus inside of menus.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">struct ContentView: View {
    var body: some View {
        
        Menu("Create") {
            Button("Cancel", action: {})
            Menu("More") {
                    Button("Rename", action: {})
                    Button("Developer Mode", action: {})
                }
            Button("Search", action: {})
            Button("Add", action: {})
        }
    }
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:image {"align":"center","id":928,"width":316,"height":614,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/09/screen-shot-2020-09-02-at-12.16.09-pm.png?w=526" alt="" class="wp-image-928" width="316" height="614"/></figure></div>
<!-- /wp:image -->

<!-- wp:image {"align":"center","id":930,"width":314,"height":613,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/09/screen-shot-2020-09-02-at-12.17.08-pm.png?w=530" alt="" class="wp-image-930" width="314" height="613"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now if we tap on the Create button and then tap our More button we now get presented with our new menu inside of our main menu.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Lastly we can have customized text and icons in our menu by adding a label to our menu. </p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">struct ContentView: View {
    var body: some View {
        
        Menu {
            Button("Cancel", action: {})
            Button("Search", action: {})
            Button("Add", action: {})
        } label: {
            Label("Create", systemImage: "plus.circle")
        }
    }
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:image {"align":"center","id":934,"width":314,"height":613,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/09/screen-shot-2020-09-02-at-12.26.12-pm.png?w=516" alt="" class="wp-image-934" width="314" height="613"/></figure></div>
<!-- /wp:image -->

<!-- wp:image {"align":"center","id":935,"width":316,"height":614,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://swifttomcom.files.wordpress.com/2020/09/screen-shot-2020-09-02-at-12.27.07-pm.png?w=528" alt="" class="wp-image-935" width="316" height="614"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>As you can see in the above example now we can add a custom title and icon to our menu button.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There is a great<a href="https://developer.apple.com/videos/play/wwdc2020/10205"> WWDC video</a> that can help explain menus if you are still confused.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I hope this helps you better understand how to use the menus in iOS 14 and SwiftUI 2.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>💻👨🏻‍💻 Happy coding! 💻👨🏻‍💻</p>
<!-- /wp:paragraph -->