forked and added changes (tab feature and restore view controller feature)
This commit is contained in:
@@ -6,15 +6,15 @@
|
||||
//
|
||||
|
||||
/* Example usage:
|
||||
// Define the menus
|
||||
SideMenuManager.menuLeftNavigationController = storyboard!.instantiateViewController(withIdentifier: "LeftMenuNavigationController") as? UISideMenuNavigationController
|
||||
SideMenuManager.menuRightNavigationController = storyboard!.instantiateViewController(withIdentifier: "RightMenuNavigationController") as? UISideMenuNavigationController
|
||||
|
||||
// Enable gestures. The left and/or right menus must be set up above for these to work.
|
||||
// Note that these continue to work on the Navigation Controller independent of the View Controller it displays!
|
||||
SideMenuManager.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
|
||||
SideMenuManager.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)
|
||||
*/
|
||||
// Define the menus
|
||||
SideMenuManager.menuLeftNavigationController = storyboard!.instantiateViewController(withIdentifier: "LeftMenuNavigationController") as? UISideMenuNavigationController
|
||||
SideMenuManager.menuRightNavigationController = storyboard!.instantiateViewController(withIdentifier: "RightMenuNavigationController") as? UISideMenuNavigationController
|
||||
|
||||
// Enable gestures. The left and/or right menus must be set up above for these to work.
|
||||
// Note that these continue to work on the Navigation Controller independent of the View Controller it displays!
|
||||
SideMenuManager.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
|
||||
SideMenuManager.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)
|
||||
*/
|
||||
|
||||
open class SideMenuManager : NSObject {
|
||||
|
||||
@@ -30,7 +30,7 @@ open class SideMenuManager : NSObject {
|
||||
let appWindowRect = UIApplication.shared.keyWindow?.bounds ?? UIWindow().bounds
|
||||
return appWindowRect
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
The presentation mode of the menu.
|
||||
|
||||
@@ -42,6 +42,12 @@ open class SideMenuManager : NSObject {
|
||||
*/
|
||||
open static var menuPresentMode: MenuPresentMode = .viewSlideOut
|
||||
|
||||
/// Retrieves each already created view controller to front when called. Requires class restoration identifiers. Defaults to false.
|
||||
open static var menuPreserveViewOnPush = false
|
||||
|
||||
/// Mimics a tab function, by instantly presenting the view controller. Defaults to false.
|
||||
open static var menuTabMode = false
|
||||
|
||||
/// Prevents the same view controller (or a view controller of the same class) from being pushed more than once. Defaults to true.
|
||||
open static var menuAllowPushOfSameClassTwice = true
|
||||
|
||||
@@ -251,7 +257,7 @@ open class SideMenuManager : NSObject {
|
||||
|
||||
- Parameter toView: The view to add gestures to.
|
||||
- Parameter forMenu: The menu (left or right) you want to add a gesture for. If unspecified, gestures will be added for both sides.
|
||||
|
||||
|
||||
- Returns: The array of screen edge gestures added to `toView`.
|
||||
*/
|
||||
@discardableResult open class func menuAddScreenEdgePanGesturesToPresent(toView: UIView, forMenu:UIRectEdge? = nil) -> [UIScreenEdgePanGestureRecognizer] {
|
||||
|
||||
@@ -97,22 +97,22 @@ open class UISideMenuNavigationController: UINavigationController {
|
||||
SideMenuTransition.statusBarView?.isHidden = true
|
||||
coordinator.animate(alongsideTransition: { (context) -> Void in
|
||||
SideMenuTransition.presentMenuStart(forSize: size)
|
||||
}) { (context) -> Void in
|
||||
SideMenuTransition.statusBarView?.isHidden = false
|
||||
}) { (context) -> Void in
|
||||
SideMenuTransition.statusBarView?.isHidden = false
|
||||
}
|
||||
}
|
||||
|
||||
override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||
if let menuViewController: UINavigationController = SideMenuTransition.presentDirection == .left ? SideMenuManager.menuLeftNavigationController : SideMenuManager.menuRightNavigationController,
|
||||
let presentingViewController = menuViewController.presentingViewController as? UINavigationController {
|
||||
presentingViewController.prepare(for: segue, sender: sender)
|
||||
presentingViewController.prepare(for: segue, sender: sender)
|
||||
}
|
||||
}
|
||||
|
||||
override open func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
||||
if let menuViewController: UINavigationController = SideMenuTransition.presentDirection == .left ? SideMenuManager.menuLeftNavigationController : SideMenuManager.menuRightNavigationController,
|
||||
let presentingViewController = menuViewController.presentingViewController as? UINavigationController {
|
||||
return presentingViewController.shouldPerformSegue(withIdentifier: identifier, sender: sender)
|
||||
return presentingViewController.shouldPerformSegue(withIdentifier: identifier, sender: sender)
|
||||
}
|
||||
|
||||
return super.shouldPerformSegue(withIdentifier: identifier, sender: sender)
|
||||
@@ -125,7 +125,7 @@ open class UISideMenuNavigationController: UINavigationController {
|
||||
super.pushViewController(viewController, animated: animated)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
let tabBarController = presentingViewController as? UITabBarController
|
||||
guard let navigationController = (tabBarController?.selectedViewController ?? presentingViewController) as? UINavigationController else {
|
||||
print("SideMenu Warning: attempt to push a View Controller from \(presentingViewController.self) where its navigationController == nil. It must be embedded in a Navigation Controller for this to work.")
|
||||
@@ -147,6 +147,11 @@ open class UISideMenuNavigationController: UINavigationController {
|
||||
if SideMenuManager.menuAllowPopIfPossible {
|
||||
for subViewController in navigationController.viewControllers {
|
||||
if type(of: subViewController) == type(of: viewController) {
|
||||
if SideMenuManager.menuTabMode {
|
||||
navigationController.popToViewController(subViewController, animated: false)
|
||||
CATransaction.commit()
|
||||
return
|
||||
}
|
||||
navigationController.popToViewController(subViewController, animated: animated)
|
||||
CATransaction.commit()
|
||||
return
|
||||
@@ -154,10 +159,40 @@ open class UISideMenuNavigationController: UINavigationController {
|
||||
}
|
||||
}
|
||||
|
||||
if SideMenuManager.menuPreserveViewOnPush {
|
||||
var viewControllers = navigationController.viewControllers
|
||||
let filtered = viewControllers.filter{$0.restorationIdentifier == viewController.restorationIdentifier}
|
||||
if let preservedView = filtered.first {
|
||||
viewControllers = viewControllers.filter() { $0 !== preservedView }
|
||||
viewControllers.append(preservedView)
|
||||
if SideMenuManager.menuTabMode {
|
||||
navigationController.setViewControllers(viewControllers, animated: false)
|
||||
CATransaction.commit()
|
||||
return
|
||||
}
|
||||
navigationController.setViewControllers(viewControllers, animated: animated)
|
||||
CATransaction.commit()
|
||||
return
|
||||
}
|
||||
if SideMenuManager.menuTabMode {
|
||||
navigationController.pushViewController(viewController, animated: false)
|
||||
CATransaction.commit()
|
||||
return
|
||||
}
|
||||
navigationController.pushViewController(viewController, animated: animated)
|
||||
CATransaction.commit()
|
||||
return
|
||||
}
|
||||
|
||||
if SideMenuManager.menuReplaceOnPush {
|
||||
var viewControllers = navigationController.viewControllers
|
||||
viewControllers.removeLast()
|
||||
viewControllers.append(viewController)
|
||||
if SideMenuManager.menuTabMode {
|
||||
navigationController.setViewControllers(viewControllers, animated: false)
|
||||
CATransaction.commit()
|
||||
return
|
||||
}
|
||||
navigationController.setViewControllers(viewControllers, animated: animated)
|
||||
CATransaction.commit()
|
||||
return
|
||||
@@ -168,6 +203,12 @@ open class UISideMenuNavigationController: UINavigationController {
|
||||
return
|
||||
}
|
||||
|
||||
if SideMenuManager.menuTabMode {
|
||||
navigationController.pushViewController(viewController, animated: false)
|
||||
CATransaction.commit()
|
||||
return
|
||||
}
|
||||
|
||||
navigationController.pushViewController(viewController, animated: animated)
|
||||
CATransaction.commit()
|
||||
}
|
||||
|
||||
@@ -130,6 +130,12 @@ There are four modes in MenuPresentMode:
|
||||
*/
|
||||
open static var menuPresentMode: MenuPresentMode = .viewSlideOut
|
||||
|
||||
/// Retrieves each already created view controller to front when called. Requires class restoration identifiers. Defaults to false.
|
||||
open static var menuPreserveViewOnPush = false
|
||||
|
||||
/// Mimics a tab function, by instantly presenting the view controller. Defaults to false.
|
||||
open static var menuTabMode = false
|
||||
|
||||
/// Prevents the same view controller (or a view controller of the same class) from being pushed more than once. Defaults to true.
|
||||
open static var menuAllowPushOfSameClassTwice = true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user