{"id":2288,"date":"2022-05-05T15:10:23","date_gmt":"2022-05-05T15:10:23","guid":{"rendered":"https:\/\/sezeromer.com\/?p=2288"},"modified":"2023-03-04T14:37:57","modified_gmt":"2023-03-04T11:37:57","slug":"swift-ile-uimenu-nasil-kullanilir","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/swift-ile-uimenu-nasil-kullanilir\/","title":{"rendered":"How to use UIMenu with Swift?"},"content":{"rendered":"<p>Hello friends, in this article we will talk about how to use <strong>UIMenu<\/strong> with <strong>Swift<\/strong>. For more details on UIMenu, you can access Apple&#8217;s documentation <a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uimenu\">here<\/a>.<\/p>\n<p>On some screens, especially on the detail screens, there may be more than one function and it may be necessary to collect them in one place. For example, there may be buttons on this screen such as share, report, share, delete, edit. From a UX point of view it&#8217;s usually good to have these buttons in the <strong>NavigationBar<\/strong>. We can add more buttons and then open them as sub-buttons after pressing this button.<\/p>\n<p>Our example will be like this. We will add more buttons to the NavigationBar and provide options under it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2289\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-05-at-17.47.52.png\" alt=\"\" width=\"385\" height=\"278\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-05-at-17.47.52.png 385w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-05-at-17.47.52-300x217.png 300w\" sizes=\"(max-width: 385px) 100vw, 385px\" \/><\/p>\n<p>First of all, I create my actions that will appear in the NavigationBar section. As you can see in the image, I have 5 actions. I set the icons of these actions with <strong>SFSymbol<\/strong>. While creating an action, we can add only images or text only, or we can add both visual and text as I did. After adding the texts and images, you can add whatever you want them to do when you click on these actions. I&#8217;m just printing something to the console for now.<\/p>\n<p>Then we need to create a menu and add these actions to this menu. While creating the menu, you can enter whatever you want in the title section and then add the actions to the children section.<\/p>\n<p>Finally, we add a BarButton to the NavigationBar. We just put an icon on this BarButton. Then we add the menu we created to this button, for now I left the <strong>primaryAction<\/strong> part of this button as nil. You can define any action here. But this time, when you click this button, this action will run first. If you press long, this time other menus will be displayed. You need to set the primaryAction part to nil for a direct menu to appear.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/ ViewController.swift\r\n\/\/ uimenu\r\n\/\/\r\n\/\/ Created by \u00d6mer Sezer on 5.05.2022.\r\n\/\/\r\n\r\nimport UIKit\r\nclass ViewController: UIViewController {\r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        configure()\r\n        \r\n    }\r\n    \r\n}\r\nfileprivate extension ViewController {\r\n    func configure() {\r\n        self.title = \"UIMenu Sample\"\r\n        setMoreButton()\r\n        \r\n    }\r\n    func setMoreButton() {\r\n        let actionShare = UIAction(title: \"Share\", image: .share) { action in\r\n            print(\"action share clicked\")\r\n        }\r\n        let actionAdd = UIAction(title: \"Add\", image: .add) { action in\r\n            print(\"action add clicked\")\r\n            \r\n        }\r\n        let actionEdit = UIAction(title: \"Edit\", image: .edit) { action in\r\n            print(\"action edit clicked\")\r\n            \r\n        }\r\n        let actionReport = UIAction(title: \"Report\", image: .report) {action in\r\n            print(\"action report clicked\")\r\n            \r\n        }\r\n        let actionDelete = UIAction(title: \"Delete\", image: .delete) { action in\r\n            print(\"action delete clicked\")\r\n            \r\n        }\r\n        let menu = UIMenu(title: \"\", children: [actionShare, actionAdd, actionEdit, actionReport, actionDelete])\r\n        navigationItem.rightBarButtonItem = UIBarButtonItem(title: nil, image: .more, primaryAction: nil, menu: menu)\r\n        \r\n    }\r\n    \r\n}<\/pre>\n<\/div>\n<p>The BarButton we created works as in the gif.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2290\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/05\/5.gif\" alt=\"\" width=\"409\" height=\"907\" \/><\/p>\n<p>You can find the sample project on <a href=\"https:\/\/github.com\/omersezer\/swift-projects\">Github<\/a>. You can find more articles about Swift <a href=\"https:\/\/sezeromer.com\/swift\/\">here<\/a>. If you have questions, you can reach us by sending an e-mail or comment.<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends, in this article we will talk about how to use UIMenu with Swift. For more details on UIMenu, you can access Apple&#8217;s documentation here. On some screens, especially on the detail screens, there may be more than one function and it may be necessary to collect them in one place. For example, there [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2289,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327,706],"tags":[421,894,115,891,8,440,893,328,707,892,282],"class_list":["post-2288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-swiftui","tag-apple","tag-development","tag-ios","tag-menu","tag-omer","tag-sezer","tag-sfsymbol","tag-swift","tag-swiftui","tag-uimenu","tag-xcode"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2288"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=2288"}],"version-history":[{"count":5,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2288\/revisions"}],"predecessor-version":[{"id":2688,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2288\/revisions\/2688"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/2289"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=2288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=2288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=2288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}