doyeona
Published in

doyeona

Context menus in iOS13+ [CollectionView]

Use context menus to give people access to additional functionality related to onscreen items without cluttering the interface which is similar to Peek and Pop.

using context menus

📌 Peek and Pop: Accelerate actions in your app by providing shortcuts to preview content in detail view controllers.

Apple is replacing Peek and Pop with context menus in iOS13 and Why is it?

Apple introduced Peek and Pop in iOS 9 and Apple never managed to add 3D touch to the iPad and they have removed it from iPhone models.

Two key differences are:

  • Context menus are available on all devices running iOS 13 and later however Peek and Pop is only available on devices that support 3D Touch.
  • Context menus immediately display relevant commands however Peek and Pop requires a swipe up to view commands.

How to use it?

I added a context menu to the collectionView to edit and delete each cell. For you to do that, simply conform the protocol UICollectionViewDelegate. If not in collectionView, create UIContextMenuInteraction and add it to a view.

collectionView(_:contextMenuConfigurationForItemAt:point:)

Returns a context menu configuration for the item at a point.

So what i need to do is call another method configureContextMenu which returns UIContextMenuConfiguration and create menus to show. This is a very similar way to creating ActionSheet.

  • UIContextMenuConfiguration: An object containing the configuration details for the contextual menu.
  • Before displaying a contextual menu, the system asks your UIContextMenuInteraction Delegate to provide a UIContextMenuConfiguration object with details about the menus.

So i first created an object containing the configuration details of the menus in #2.

After that, add menus such as share, delete, favorite and many others. Next is to set the title and icons. If you have some other tasks when the menu is pressed, do it in here refer to line #4 to 9

When everything is set up, return UIMenu with the title of the menu and the menus that you just created such as edit, delete or add… in line # 11

Here’s the complete code:

But I think it’s very important to understand the Human Interface Guideline about Context Menu before trying it on your side. By just using context menu has great advantages and fast access to the content without opening it and digging into other options however, people don’t use it very often like peek and pop because they don’t know what are the features available for your app and it’s kinda hidden. Therefore it might be hard to configure the app features if you hide them in context menus.💦\

references:

--

--

--

Daily study logs about iOS development will be updated. Any recommendations or discussions are always welcome

Recommended from Medium

SWIFTip#6: Casual insight of the week

SwiftUI: view modifier to set global (hole screen relative) position of the view

Deploy Flutter Web App to Azure App Service with Node.js

What are The Top 8 Must-Have Skills For a Swift Developer In 2020

EnvironmentObject usage in SwiftUI

TDD on Swift: A practical perspective developing Equations

iOS — Separation of XCUnit & XCUITest jobs in CircleCI for single & multi app regions/variants

Building a tool as an alternative to stand ups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Doyeon

Doyeon

Hey! I am currently studying iOS by myself, and wanna be a cool app developer 🕶 please correct me if i’m wrong or if there’s any thing that you wanna share😊✌️

More from Medium

Variable Definition, Arrays, Dictionaries and Set Structures in Swift

Content Hugging & Content Compression Resistance Priority [iOS — CHCR priorities]

What is a Lazy Stored Property and when to use it?

Prevent Homebrew services from starting automatically