iOS Interactive Side Menu written in Swift.
It supports following customization:
- Animation duration
- Visible content width
- Content scale
- Using spring animation with params customization
- Animation options like animation curve
All these parameters could vary for different orientations.
To install it through CocoaPods, add the following line to your Podfile:
pod 'InteractiveSideMenu'
To install it through Carthage, add the following line to your Cartfile:
github "handsomecode/InteractiveSideMenu"
To implement your side menu you should create subclasses of basic View Controllers.
MenuContainerViewControlleris a host for menu and content viewsMenuViewControlleris a container for menu view
Also, ensure that every menu item ViewController adopts relevant protocol.
SideMenuItemContentis a ViewController's protocol for data that corresponds menu item
To setup your side menu you need to do three things:
- Provide implementation of base
MenuViewControllerand assing it tomenuViewControllerproperty - Provide implementation of menu content and assing array of content controllers to
contentViewControllersproperty - Select initial content controller by calling
selectContentViewController(_ selectedContentVC: MenuItemContentViewController)
import InteractiveSideMenu
class HostViewController: MenuContainerViewController {
override func viewDidLoad() {
super.viewDidLoad()
menuViewController = self.storyboard!.instantiateViewController(withIdentifier: "NavigationMenu") as! MenuViewController
contentViewControllers = contentControllers()
selectContentViewController(contentViewControllers.first!)
}
private func contentControllers() -> [MenuItemContentViewController] {
var contentList = [MenuItemContentViewController]()
contentList.append(self.storyboard?.instantiateViewController(withIdentifier: "First") as! MenuItemContentViewController)
contentList.append(self.storyboard?.instantiateViewController(withIdentifier: "Second") as! MenuItemContentViewController)
return contentList
}
}To show menu, call showSideMenu() method from SideMenuItemContent protocol.
import InteractiveSideMenu
class KittyViewController: UIViewController, SideMenuItemContent {
@IBAction func openMenu(_ sender: UIButton) {
showSideMenu()
}
}To change content view, choose desired content controller and hide menu.
let index = 2 // second menu item
guard let menuContainerViewController = self.menuContainerViewController else { return }
let contentController = menuContainerViewController.contentViewControllers[index]
menuContainerViewController.selectContentViewController(contentController)
menuContainerViewController.hideMenu()To customize animation for menu opening or closing, update transitionOptions property that is available in MenuContainerViewColtroller class. Initial setup could be done, for example, on controller's viewDidLoad().
override func viewDidLoad() {
super.viewDidLoad()
let screenSize: CGRect = UIScreen.main.bounds
self.transitionOptions = TransitionOptions(duration: 0.4, visibleContentWidth: screenSize.width / 6)
...
}Also, you have possibility to update customization settings, e.g. set another options for landscape orientation. To do it, override viewWillTransition(to:with:) mehod and add desired parameters to transitionOptions property.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
var options = TransitionOptions()
options.duration = size.width < size.height ? 0.4 : 0.6
options.visibleContentWidth = size.width / 6
self.transitionOptions = options
}Transition options could be used to set different parameters for Compact and Regular sizes as well. Implement ViewController's traitCollectionDidChange(_: ) method to add these settings.
See Sample for more details.
- iOS 8.0+
- Xcode 8.1+
- Swift 3.0+
InteractiveSideMenu is available under the Apache License, Version 2.0. See the LICENSE file for more info.
