PEEK AND POP FOR UITABLEVIEW

lyvennitha sasikumar
2 min readApr 1, 2021

Peek and Pop is a feature that is accessible from iPhone 6s and above. It is an action when user long press any UI elements on the screen we able to show a preview or quick menu actions by highlighting the pressed UI Element.So from this feature user can sense the 3D touch.

The Core Spell:

  1. Create tableView in storyboard.
  2. Implement delegate and data source methods for tableView.
  3. This is the main step where we implement the Contextual menu delegates of tableView.

Below is the storyboard design:

StoryBoard Reference

Implementing Essential Delegate and DataSource of UITableView:

Here we are going to show Menu option(UIContextualMennu) for peek a pop view having light and dark option which made themes according to the action.

Below is the tableView essential Delegate and DataSource methods:

extension ViewController: UITableViewDelegate, UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 5

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: “Cell”, for: indexPath)

return cell

}

}

Below is the output image for tableView:

Below is the final step, here we are going to implement contextual menu delegate for tableViewCell

func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {

let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu? in

let action = UIAction(title: “Dark Mode”, image: nil, identifier: .none) { action in

self.view?.overrideUserInterfaceStyle = .dark

}

let action2 = UIAction(title: “Light Mode”, image: nil, identifier: .none) { action in

self.view?.overrideUserInterfaceStyle = .light

}

return UIMenu.init(title: “SELECT MODE”, children: [action, action2])

}

return configuration

}

Below is the output images for peek and pop tableView:

peek and pop tableView - light mode
peek and pop tableView — dark mode

Note: The above UIContextMenuConfiguration delegate available after iOS 13.0. Please check before implement this.

You can follow me here to get more occasional content.
Gimme a clap if you like it. Don’t hang back to contact me at any time. Mail your questions to lyvennithasasikumar@gmail.com.

Happy Coding😊

--

--

lyvennitha sasikumar

Passionate iOS developer chasing my dreams toward success