SideMenu is an easy-to-use container view controller written in Swift.
Besides all the features a SideMenu 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
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.
To set up SideMenu
in storyboard:
- Open the initial view controller's identity inspector. Change it's Class to
SideMenuController
and change it's Module toSideMenuSwift
. - Set up the menu view controller and the initial content view controller in your Storyboard. Add a Custom segue from the
SideMenuController
to both of them.- Change the menu segue's identifier to
SideMenu.Menu
, change it's Class toSideMenuSegue
and change it's Module toSideMenuSwift
. - Change the content segue's identifier to
SideMenu.Content
, change it's Class toSideMenuSegue
and change it's Module toSideMenuSwift
.
- Change the menu segue's identifier to
- (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.
- Open the
- It's done.
To start the app with SideMenu
:
import UIKit
import SideMenuSwift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let contentViewController = ...
let menuViewController = ...
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = SideMenuController(contentViewController: contentViewController, menuViewController: menuViewController)
window?.makeKeyAndVisible()
return true
}
}
Use sm_sideMenuController
provides in UIViewController
's extension to access the parent SideMenuController
:
viewController.sm_sideMenuController.revealMenu()
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.
One of the biggest 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
sm_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:
sm_sideMenuController.cache(viewControllerGenerator: { self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") }, with: "1")
sm_sideMenuController.cache(viewControllerGenerator: { self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") }, with: "2")
- Xcode 9
- iOS 9 or later
To install SideMenu
with CocoaPods, add the below line in your Podfile
:
pod 'SideMenuSwift'
# Note its not 'SideMenu'
To install SideMenu
with Carthage, add the below line in your Cartfile
:
github "kukushi/SideMenu" "master"
SideMenu is available under the MIT license. See the LICENSE file for more info.