Accelerate actions in your app by providing shortcuts to preview content in detail view controllers.
- iOS 11.4+
- Xcode 10.0+
This sample project demonstrates previewing content and providing quick shortcuts to functionality using Peek and Pop APIs. The app uses a table view to display a list of colors and tapping any row in the table navigates to a detail view controller showing the color. The app uses 3D Touch on the table view rows to allow users to peek at the content without placing it on the navigation stack.
Colors can also be starred or unstarred. Starred colors are shown with a solid filled colored star icon to the left of the name and unstarred colors have a hollow star. Users can toggle whether a color is starred using one of two methods: they can either tap a button in the navigation bar of the detail view, or use 3D Touch to peek at the content. Swiping up while in 3D Touch lets the user access a Peek Quick Action.
The app uses a tab bar controller to demonstrate two different methods for implementing Peek and Pop. The first tab contains the
Colors view controller, which configures Peek and Pop using the segue in
Main. The second tab contains the
Colors view controller to achieve the same functionality, but in code.
Colors are subclasses of
Colors, which contains the implementation of everything necessary to display the table view and handle the standard segue.
Peek and Pop from a Storyboard
The storyboard uses a Show segue with Peek and Pop enabled to navigate from the
Color. This is done by selecting the Show segue, and turning on “Preview & Commit Segues” next to Peek & Pop in the Attributes inspector.
When configuring the
Color, enable the “Use Preferred Explicit Size” option in the View Controller section of the Attributes inspector. Set the “Content Size” to have 0 width and an appropriate height. Zero width configures the view controller to automatically resize to the device width when the user peeks at the content.
When implementing the
Colors view controller, resist the temptation of using either
index when preparing to execute a segue. If you configure the storyboard with the default implementation of Peek and Pop, these methods return
nil at the time of segue execution. Instead, use the segue’s
sender property to access the cell initiating the peek.
Peek and Pop in Code
Colors view controller adds code to manually register for Peek and Pop instead of using the storyboard to customize the segue. Call
register to register a class which implements the
UIView protocol, and then pass in a view which responds to the 3D Touch.
The protocol requires the implementation of two methods. When the system detects the 3D Touch, it calls
previewing, passing a
previewing object that conforms to the
UIView protocol. Use this method to configure and pass back a view controller for the peek.
When the system detects enough pressure in the 3D Touch to pop the view controller, it calls
previewing. Take the passed in view controller and present to the user.
Implement Peek Quick Actions
To enable Peek Quick Actions for the
Color, you must override the default implementation of the
preview property to return an array of
Both a star/unstar action and a delete action are available as quick actions using the following code:
The star/unstar action is shown with a default style and the delete action is shown as being destructive.
It’s important to ensure that your app’s features are still available to devices that do not support 3D Touch. In this sample, both the star/unstar and the delete functionality remain available by navigating into the
Color and tapping buttons in the navigation bar. The Peek Quick Action provides a more convenient way to access that feature.