This context provides a tutorial on implementing context menus and SF Symbols in iOS 13 using Collection Views.
Abstract
The article discusses the implementation of context menus and SF Symbols in iOS 13 using Collection Views. It explains the differences between context menus and 3D Touch Peek and Pop gestures, and provides code examples for setting up context menus on views. The article also covers the use of SF Symbols, which provides system image names, and shows how to customize the size and style of system icons. The tutorial demonstrates the implementation of context menus on a UICollectionView, and covers the use of UIContextMenuConfiguration and UICollectionViewDelegate methods. The article concludes with an example of displaying a target view controller in a preview along with the context menu on cell press.
Opinions
Context menus are a significant development in iOS 13 and are promoted as the replacement for 3D Touch.
SF Symbols is a newly introduced Mac application that provides an encyclopedia of system icon names.
The tutorial demonstrates the implementation of context menus on a UICollectionView, which is a common use case for iOS developers.
The article covers the use of UIContextMenuConfiguration and UICollectionViewDelegate methods, which are important for implementing context menus in iOS 13.
The article concludes with an example of displaying a target view controller in a preview along with the context menu on cell press, which is a useful feature for iOS developers.
iOS 13 Context Menus and SF Symbols
The replacement for 3D Touch is here to stay
Our final outcome at the end of this tutorial
Context menus were one of the significant developments in this year’s WWDC 2019 for iOS 13. Promoted as the replacement for 3D touch, it’s here to stay!
The goal of this article is the implementation of context menus and SF Symbols. So let’s dive right in.
Plan of Action
We’ll be covering the implementation of context menus using Collection Views.
We will be using SF Symbols, which provides plenty of system image names.
Context Menus vs. Peek and Pop
Context menus introduced with iOS 13 are not the same as 3D Touch Peek and Pop gestures.
Following are the key differences:
Peek and Pop works on devices that support 3D Touch, whereas context menus are for all devices.
Context menus are displayed on a long press or force touch (if the device supports it), whereas a Peek and Pop menu is shown on force touch only.
Peek and Pop menus require you to swipe up to view the menu, whereas context menus are visible right there where you long-press.
To setup ContextMenu on a View, we need to set upUIContextMenuIteraction on it. Also, we need to conform to UIContextMenuInteractionDelegate
Basic Setup of a Context Menu on a View
Let’s take a glance at the important delegate methods in the UIContextMenuInteractionDelegate protocol:
Initialization of a Context Menu
To initialize a Context Menu, we need to set three arguments (all are optional):
identifier - You can set a string id here.
previewProvider - A preview of the next View Controller
actionProvider - We create our menu buttons and actions here.
SFSymbols is a newly introduced Mac application that is essentially an encyclopedia of system icon names. We can pass those names in UIImage(systemName:) initializer introduced with iOS 13.
Customising System Icons
We can customize the size/style of the system icons by passing a configuration argument as shown below:
let config =UIImage.SymbolConfiguration(textStyle: .largeTitle)
In the following sections, we’ll be implementing ContextMenu on a UICollectionView.
From Apple Docs
iOS 13 introduces a few new UICollectionViewDelegate methods for context menus. In order to display a ContextMenu when a user long-presses on a CollectionView cell, the following function is used: