{"id":2841,"date":"2023-12-03T18:26:56","date_gmt":"2023-12-03T15:26:56","guid":{"rendered":"https:\/\/sezeromer.com\/?p=2841"},"modified":"2023-12-03T18:28:55","modified_gmt":"2023-12-03T15:28:55","slug":"swift-ile-uicontextmenu-nasil-kullanilir","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/swift-ile-uicontextmenu-nasil-kullanilir\/","title":{"rendered":"How to Use UIContextMenu with Swift?"},"content":{"rendered":"<p>Hello friends, in this article we will talk about how to use UIContextMenu with <strong>Swift<\/strong>. First of all, what is <strong>UIContextMenu<\/strong> and why is it used? we will talk about this.<\/p>\n<p><strong>UIContextMenu<\/strong> is an API that will allow different actions to be taken when an object on the screen is touched or held. The example I will talk about here will be from my own application. In the <strong>My Plants<\/strong> application, the user may want to delete the image of his own plant. While doing this, we need to do it in a way that iOS users are familiar with. So we can do the deletion using UIContextMenu. By the way, you can download the My Plants application from <a href=\"https:\/\/apps.apple.com\/tr\/app\/bitkilerim\/id1562973444?l=tr\">here<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2842 aligncenter\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2023\/12\/Simulator-Screen-Recording-iPhone-15-Pro-2023-12-03-at-17.40.36.gif\" alt=\"\" width=\"295\" height=\"640\" \/><\/p>\n<p>The My Plants detail screen contains photos of the user&#8217;s plants by date. Here we show the photos uploaded by the user in the UICollectionView. If you press and hold any of the Cells containing these photos, we want the delete option to appear. Then we perform the deletion process.<\/p>\n<p>There is a function for UIContextMenu in <strong>UICollectionViewDataSource<\/strong>. With this function, we can give this feature to Cells. Note that this function supports iOS 16 and above. You can use the function below to create a structure like the Gif above.<\/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;}\">    func collectionView(_ collectionView: UICollectionView,\r\n                        contextMenuConfigurationForItemsAt indexPaths: [IndexPath],\r\n                        point: CGPoint) -&gt; UIContextMenuConfiguration? {\r\n        return UIContextMenuConfiguration(identifier: nil,\r\n                                          previewProvider: nil) { [weak self] _ in\r\n            let deleteAction = UIAction(title: NSLocalizedString(\"Delete\", comment: \"\"),\r\n                                        image: UIImage(systemName: \"trash\"),\r\n                                        attributes: .destructive) { _ in\r\n                self?.delegate?.onDeleteButtonClicked(for: indexPaths.first?.row)\r\n            }\r\n\r\n            return UIMenu(title: \"\", children: [deleteAction])\r\n        }\r\n    }<\/pre>\n<\/div>\n<p>You can also add different actions under UIMenu Children.<\/p>\n<p>If you have questions, you can reach us by e-mail or comment. You can find more Swift-related articles <a href=\"https:\/\/sezeromer.com\/swift\/\">here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends, in this article we will talk about how to use UIContextMenu with Swift. First of all, what is UIContextMenu and why is it used? we will talk about this. UIContextMenu is an API that will allow different actions to be taken when an object on the screen is touched or held. The example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2843,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327,706],"tags":[985,982,984,981,8,7,440,328,707,983],"class_list":["post-2841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-swiftui","tag-apps","tag-bitkilerim","tag-delete","tag-my-plants","tag-omer","tag-omer-sezer","tag-sezer","tag-swift","tag-swiftui","tag-uicontextmenu"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2841"}],"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=2841"}],"version-history":[{"count":3,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2841\/revisions"}],"predecessor-version":[{"id":2846,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2841\/revisions\/2846"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/2843"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=2841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=2841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=2841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}