I had a hard time implementing just as a simple feature that you can find generally on any mobile app, a swipeout menu feature with a gesture.

🚨 Spoiler Alert!
I use 100% programmatic approach to build this project and yes no storyboard or xib file. Source code on Github

As I am not using storyboard I need to manually set up the initial view controller. So, let us head to AppDelegate.swift file and add the following code on function application(_:didFinishLaunchingWithOptions:).

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow()
    window?.makeKeyAndVisible()
    window?.rootViewController = UINavigationController(rootViewController: HomeController())
     
    return true
  }

Here in window?.rootViewController we set the initial view controller with UINavigationController to HomeController(). Create a new file HomeController.swift and add the following code.

import UIKit

class HomeController: UITableViewController {
   
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.backgroundColor = .red
  }
  
  //MARK:- TableView Datasource
   
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
  }
   
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cellId")
    cell.textLabel?.text = "Item \(indexPath.row)"
    return cell
  }
}

We are making our HomeController class a UITableViewController as for now. Since the HomeController inherits the UITableViewController class the data source and delegate for the table view is itself. But we must provide the number of data there are and what are its data. So we used the functions tableView(:numberOfRowsInSection section:) to tell the number of rows in a table view, here 5, and tableView(:cellForRowAt:) to tell what the table view cell contains, here with the label “Item 0” for the first cell, “Item 1” for the second cell, “Item 2” for the third cell and so forth. We set the background colour of the view HomeController to red.

initial-img

Now we will setup the navigation bar by the following function. You can write the code, which is inside of setupNavigationItems(), to viewDidLoad() function instead of creating setupNavigationItems() function and calling setupNavigationItems() to viewDidLoad(). Generally, people like to clean their viewDidLoad() as possible.

fileprivate func setupNavigationItems() {
    title = "Home"
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Open", style: .plain, target: self, action: #selector(openMenu))
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Hide", style: .plain, target: self, action: #selector(hideMenu))
  }

After this, we will create two functions openMenu() and hideMenu(). To know why to use @objc tag click here.

  @objc func openMenu() {
    performAnimation(transform: CGAffineTransform(translationX: self.menuWidth, y: 0))
  }

  @objc func hideMenu() {
    performAnimation(transform: .identity)
  }

fileprivate func performAnimation(transform: CGAffineTransform) {
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
      self.menuController.view.transform = transform
      self.navigationController?.view.transform = transform
    }, completion: nil)
  }

The performAnimation() function is used for the animation effect while the menu is to open or close. what is self.menuController? its MenuController class that we haven’t created yet. So, let us create a MenuController.swift file with the following code

import UIKit

class MenuController: UITableViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .yellow
  }

  // MARK: - Table view data source
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cellId")
    cell.textLabel?.text = "Menu Item \(indexPath.row)"
     
    return cell
  }
}

It has the same function as the HomeController class as MenuController inherits the UITableViewController class. Let’s head back to HomeController and setup of the MenuController.

 let menuController = MenuController()
  fileprivate func setupMenuController() {
    menuController.view.frame = CGRect(x: 0, y: 0, width: -menuWidth, height: self.view.frame.height)
    let mainWindow = UIApplication.shared.keyWindow
    mainWindow?.addSubview(menuController.view)
    addChild(menuController)
  }

To add the swipe gesture to open and close the menu we will be using UIPanGestureRecognizer.