Navigation Drawer is a simplified sliding menu control written in swift. Look into project example to see it in action!
BaseViewController | SlideViewController |
---|---|
Button Pressed | Button Pressed |
Gesture | Gesture |
- Xcode 9.
- iOS 9 or higher.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
pod 'NavigationDrawer'
Then, run the following command:
$ pod install
-
Create a BaseViewController and add Navigation controller on it. Set Bar Button item on the view controller. Attatch this UIViewController to
BaseViewController.swift
-
Create a SlidingViewController and add Action Segue[kind: Present Modally, identifer:showSlidingMenu] from Bar Button item of
BaseViewController
toSlidingViewController
. (PS. identifer name can be anything, just match them in swift.) -
In SlidingViewCotroller add a Close Button with view Constrains as
topConstrain - As View's Top Constrain
trailingConstrain - As View's Trailing Constrain
bottomConstrain - As View's Bottom Constrain
widthConstrain - As View's width Constrain with a multipler of 0.2
- On
BaseViewController.swift
, import NavigationDrawer. Create an object of Interactor in yourBaseViewController
add two IBActionshomeButtonPressed(_ sender: UIBarButtonItem)
andedgePanGesture(sender: UIScreenEdgePanGestureRecognizer)
class ViewController: UIViewController {
//1.
let interactor = Interactor()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//2.
@IBAction func homeButtonPressed(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "showSlidingMenu", sender: nil)
}
//3. Add a Pan Gesture to slide the menu from Certain Direction
@IBAction func edgePanGesture(sender: UIScreenEdgePanGestureRecognizer) {
let translation = sender.translation(in: view)
let progress = MenuHelper.calculateProgress(translationInView: translation, viewBounds: view.bounds, direction: .Right)
MenuHelper.mapGestureStateToInteractor(
gestureState: sender.state,
progress: progress,
interactor: interactor){
self.performSegue(withIdentifier: "showSlidingMenu", sender: nil)
}
}
//4. Prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? SlidingViewController {
destinationViewController.transitioningDelegate = self
destinationViewController.interactor = self.interactor
}
}
}
- Extend the
UIViewControllerTransitioningDelegate
in your BaseViewController and add following functions.
extension ViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PresentMenuAnimator()
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return DismissMenuAnimator()
}
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactor.hasStarted ? interactor : nil
}
func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactor.hasStarted ? interactor : nil
}
}
- Create a
SlidingView.swift
and a variable of interactor that will be passed from theBaseViewController
. [Import NavigationDrawer]
class SlidingViewController: UIViewController{
var interactor:Interactor? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Handle Gesture
@IBAction func handleGesture(sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: view)
let progress = MenuHelper.calculateProgress(translationInView: translation, viewBounds: view.bounds, direction: .Left)
MenuHelper.mapGestureStateToInteractor(
gestureState: sender.state,
progress: progress,
interactor: interactor){
self.dismiss(animated: true, completion: nil)
}
}
@IBAction func closeBtnPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
Now Hook up the @IBAction
to StoryBoard.
Credits: https://www.thorntech.com/2016/03/ios-tutorial-make-interactive-slide-menu-swift/
MIT
Free Software, Hell Yeah!