/SideMenu

An interactive iOS side menu with rich features.

Primary LanguageSwiftMIT LicenseMIT

Version Swift4 Carthage compatible License Platform

Overview

SideMenu is an easy-to-use side bar style container view controller written in pure Swift.

Besides all the features a Side Menu should have, it supports:

  • Four types of status bar behaviors which adopts by most Apps
  • Three different menu position
  • Both storyboard and programmatically
  • Caching the content view controller and lazy initialization
  • Rubber band effect while panning

Preview

Menu Position / Status Bar Behavior

Above + None Below + Slide
SideBySide + Fade SideBySide + HideOnMenu

We call the left/right view controller as the menu view controller, the central view controller as content view controller.

Installation

CocoaPods

To install SideMenu with CocoaPods, add the below line in your Podfile:

pod 'SideMenuSwift'
# Note its NOT 'SideMenu'

Carthage

To install SideMenu with Carthage, add the below line in your Cartfile:

github "kukushi/SideMenu" "master"

Usages

Storyboard

To set up SideMenu in storyboard:

  1. Open the view controller's Identity inspector. Change the Class to SideMenuController and Module to SideMenuSwift.
  2. Set up the menu view controller and the initial content view controller in your Storyboard. Add a Custom segue from the SideMenuController to each of them.
    • Change the menu segue's identifier to SideMenu.Menu, Class to SideMenuSegue and Module to SideMenuSwift.
    • Change the content segue's identifier to SideMenu.Content, Class to SideMenuSegue and Module to SideMenuSwift.
  3. (Optional) If you want to use custom segue identifier:
    • Open the SideMenuController's Attribute inspector.
    • In the Side Menu Controller section, modify the Content SegueID/Menu SegueID to the desired value and change the corresponding segue's identifier.
  4. It's done. Check this screenshot a for clear view.

Programmatically

To start the app with SideMenu:

import UIKit
import SideMenuSwift
// If you are using Carthage, using `import SideMenu`

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    @objc func applicationDidFinishLaunching(_ application: UIApplication) {
        let contentViewController = ...
        let menuViewController = ...
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = SideMenuController(contentViewController: contentViewController, menuViewController: menuViewController)
        window?.makeKeyAndVisible()
        return true
    }
}

Use the sideMenuController method provided in UIViewController's extension to get the parent SideMenuController:

viewController.sideMenuController?.revealMenu()

Preferences

All the preferences of SideMenu can be found in SideMenuController.preferences. Its recommend to check out the Example to see how those options will take effect.

SideMenuController.preferences.basic.menuWidth = 240
SideMenuController.preferences.basic.statusBarBehavior = .hideOnMenu
SideMenuController.preferences.basic.position = .below
SideMenuController.preferences.basic.direction = .left
SideMenuController.preferences.basic.enablePanGesture = true
SideMenuController.preferences.basic.enablePanGesture = true
// Many other options.

Caching the Content

One of the coolest features of SideMenu is caching.

// Cache the view controllers somewhere in your code
sideMenuController?.cache(viewControllerGenerator: secondViewController, with: "1")
sideMenuController?.cache(viewControllerGenerator: thirdViewController, with: "2")

// Switch to it when needed
sideMenuController?.setContentViewController(with: "1")

What about the content view controller initialized from the Storyboard? We can use the preferences to apply a default key for it!

SideMenuController.preferences.basic.defaultCacheKey = "0"

What if we can't want to load all the content view controllers so early? We can use lazy caching:

sideMenuController?.cache(viewControllerGenerator: { self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") }, with: "1")
sideMenuController?.cache(viewControllerGenerator: { self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") }, with: "2")

Requirements

  • Xcode 9
  • iOS 9.0 or later

License

SideMenu is available under the MIT license. See the LICENSE file for more info.