Dec 23 2019
, Swift 5, iOS 13, Xcode 11Swift 5, iOS 13, Xcode 11
Learn to enhance your app with context menus, including configuring actions, adding images, nesting submenus, adding custom previews and more.
By Keegan Rush.
Once again, the default preview leaves some room for improvement. The context menu just uses the entire table view cell as a preview.
In your first context menu, you used the previewProvider parameter of the UIContextMenuConfiguration to show a custom preview. previewProvider lets you create a whole new UIViewController as a preview. But, there’s another way.
Rather than creating a new UIViewController, this uses a UITargetedPreview to specify an existing view. Here’s what’s happening, step-by-step:
In the previous method, you gave the UIContextMenuConfiguration an identifier. Now, you use it to get the tapped index.
Get the cell at that index.
Create a UITargetedPreview, passing in the cell’s image view.
This tells the context menu to use the cell’s image view as a preview, rather than the entire cell.
Build and run to see your new preview:
That looks a lot better, doesn’t it? Now, tap the preview.
The list of vacation spots animates onto the screen again. Well, the purpose of a preview is to preview something. In this case, it would make sense to preview the spot info view controller for the vacation spot. You’ll address that next.
Handling Preview Actions
Add this final UITableViewDelegate method in SpotsViewController:
This UITableViewDelegate method fires when tapping a context menu’s preview. Tapping the preview dismisses the context menu, and tableView(_:willPerformPreviewActionForMenuWith:animator:) gives you a chance to run code when the animation completes. Here’s what’s going on:
Like before, use the identifier to find the index of the row the context menu belongs to.
Get the cell that the user tapped.
The animator object handles the dismissal animation. Here, you add a completion handler that shows the spot info view controller via a segue.
Build and run, and see what happens when you tap the preview. While you’re at it, have some fun using your new context menus, because you’ve finished the tutorial. Congratulations!
Where to Go From Here?
In this tutorial, you improved the user experience of an existing app by:
Adding two distinct context menus: One on a button and one on each cell of a table view.
Adding useful actions to the menus.
Using submenus, which can keep things organized.
Customizing the menus further by adding your own previews.
Handling taps on the preview.
Context menus also interact seamlessly with drag and drop. You can learn more by watching WWDC 2019’s Modernizing Your UI for iOS 13 video.
Context menus are a different beast in SwiftUI, but much simpler. Why not get a taste with our SwiftUI: Getting Started tutorial?
I hope you’ve enjoyed this tutorial on context menus in UIKit. If you have any questions, feel free to leave them in the discussion forum below.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.