SideMenu

repository·master·Indexed 26 days ago

https://github.com/jonkykong/sidemenu

A Swift-based iOS library for creating versatile and highly customizable side menu controls. It features storyboard support, eight standard animation styles including parallax, continuous swiping gestures, and global configuration. The library uses actual view controllers for transitions rather than snapshots and supports Xcode 11+, Swift 5, and iOS 10+.

Tokens
3.7K
Snippets
10
Records
14
Agent score
41%

What's inside SideMenu

  1. Overview of SideMenu

    master

    SideMenu is a versatile side menu control for iOS written in Swift. It allows for highly customizable side menus with several key features:

    • Storyboard Support: Can be implemented in a storyboard without writing any code.
    • Animation Styles: Includes eight standard animation styles, including a parallax effect.
    • Gesture Support: Supports continuous swiping between side menus on both sides in a single gesture.
    • Global Configuration: Allows for global menu configuration that applies to all screens.
    • Custom Transitions: Menus are presented and dismissed using custom transitions, meaning they use actual view controllers rather than snapshots.
    • System Handling: Properly handles screen rotation and in-call status bar height changes.
  2. Implement SideMenu using Code

    master

    To implement SideMenu programmatically, import the module and instantiate a SideMenuNavigationController with a root view controller. You can then present it modally from any view controller.

    import SideMenu
    
    // Define the menu
    let menu = SideMenuNavigationController(rootViewController: YourViewController)
    
    // Present the menu
    present(menu, animated: true, completion: nil)
    
    // To dismiss the menu programmatically
    dismiss(animated: true, completion: nil)
  3. Implement SideMenu using Storyboards

    master

    You can implement SideMenu without code by using Storyboards:

    1. Create a Navigation Controller for the side menu. In the Identity Inspector, set its Custom Class to SideMenuNavigationController. (Set Module to SideMenu if necessary).
    2. Create a Root View Controller for this Navigation Controller and set up your menu items (e.g., using a UITableViewController).
    3. Configure the menu position: Set the Left Side property of the SideMenuNavigationController to On for a left-side menu, or Off/Default for a right-side menu.
    4. Trigger the menu: Add a UIButton or UIBarButton to your main view controller and set its action to modally present the SideMenuNavigationController created in step 1.
  4. Configure SideMenu gestures with SideMenuManager

    master

    Gestures must be enabled via code using SideMenuManager. To use gestures, you must first assign your menu controllers to SideMenuManager.default.leftMenuNavigationController or SideMenuManager.default.rightMenuNavigationController (typically in AppDelegate).

    // 1. Define and assign menus in AppDelegate
    let leftMenuNavigationController = SideMenuNavigationController(rootViewController: YourViewController)
    SideMenuManager.default.leftMenuNavigationController = leftMenuNavigationController
    
    let rightMenuNavigationController = SideMenuNavigationController(rootViewController: YourViewController)
    SideMenuManager.default.rightMenuNavigationController = rightMenuNavigationController
    
    // 2. Setup gestures (e.g., in a ViewController)
    SideMenuManager.default.addPanGestureToPresent(toView: self.navigationController!.navigationBar)
    SideMenuManager.default.addScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)
  5. Install SideMenu via CocoaPods

    master

    To integrate SideMenu using CocoaPods, add it to your Podfile.

    For Swift 5 projects, it is recommended to use version ~> 6.0.

    Note: For Swift 4.2 (no longer maintained), use ~> 5.0.

    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '10.0'
    use_frameworks!
    
    pod 'SideMenu', '~> 6.0'
  6. Implement SideMenuNavigationControllerDelegate

    master

    Adhere to SideMenuNavigationControllerDelegate in your view controller to receive lifecycle notifications when the menu is displayed or dismissed. If your view controller implements this protocol, the methods are called automatically without needing to manually set a sideMenuDelegate.

    extension MyViewController: SideMenuNavigationControllerDelegate {
    
        func sideMenuWillAppear(menu: SideMenuNavigationController, animated: Bool) {
            print("SideMenu Appearing! (animated: \(animated))")
        }
    
        func sideMenuDidAppear(menu: SideMenuNavigationController, animated: Bool) {
            print("SideMenu Appeared! (animated: \(animated))")
        }
    
        func sideMenuWillDisappear(menu: SideMenuNavigationController, animated: Bool) {
            print("SideMenu Disappearing! (animated: \(animated))")
        }
    
        func sideMenuDidDisappear(menu: SideMenuNavigationController, animated: Bool) {
            print("SideMenu Disappeared! (animated: \(animated))")
        }
    }
  7. Create a Custom SideMenuPresentationStyle

    master

    To create a custom transition, subclass SideMenuPresentationStyle and override its properties (like backgroundColor, menuScaleFactor, presentingEndAlpha, etc.) and transition methods (presentationTransitionWillBegin, dismissalTransitionDidEnd, etc.).

    class MyPresentStyle: SideMenuPresentationStyle {
    
        override init() {
            super.init()
            backgroundColor = .black
            menuStartAlpha = 1
            menuOnTop = false
            menuTranslateFactor = 0
            menuScaleFactor = 1
            onTopShadowColor = .black
            onTopShadowRadius = 5
            onTopShadowOpacity = 0
            onTopShadowOffset = .zero
            presentingEndAlpha = 1
            presentingTranslateFactor = 0
            presentingScaleFactor = 1
            presentingParallaxStrength = .zero
        }
    
        override func presentationTransitionWillBegin(to presentedViewController: UIViewController, from presentingViewController: UIViewController) {}
        override func presentationTransition(to presentedViewController: UIViewController, from presentingViewController: UIViewController) {}
        override func presentationTransitionDidEnd(to presentedViewController: UIViewController, from presentingViewController: UIViewController, _ completed: Bool) {}
        override func dismissalTransitionWillBegin(to presentedViewController: UIViewController, from presentingViewController: UIViewController) {}
        override func dismissalTransition(to presentedViewController: UIViewController, from presentingViewController: UIViewController) {}
        override func dismissalTransitionDidEnd(to presentedViewController: UIViewController, from presentingViewController: UIViewController, _ completed: Bool) {}
    }
  8. Reference: SideMenuNavigationController Properties

    master

    SideMenuNavigationController is a subclass of UINavigationController that controls the appearance, animation, and behavior of the side menu.

    var allowPushOfSameClassTwice: Bool = true
    var alwaysAnimate: Bool = true
    var animationOptions: UIView.AnimationOptions = .curveEaseInOut
    var blurEffectStyle: UIBlurEffect.Style? = nil
    var completeGestureDuration: Double = 0.35
    var completionCurve: UIView.AnimationCurve = .curveEaseInOut
    var dismissDuration: Double = 0.35
    var dismissOnPresent: Bool = true
    var dismissOnPush: Bool = true
    var dismissOnRotation: Bool = true
    var dismissWhenBackgrounded: Bool = true
    var enableSwipeToDismissGesture: Bool = true
    var enableTapToDismissGesture: Bool = true
    var initialSpringVelocity: CGFloat = 1
    var leftSide: Bool = false
    var menuWidth: CGFloat = 240
    var presentDuration: Double = 0.35
    var presentingViewControllerUserInteractionEnabled: Bool = false
    var presentingViewControllerUseSnapshot: Bool = false
    var presentationStyle: SideMenuPresentStyle = .viewSlideOut
    var pushStyle: MenuPushStyle = .default
    var statusBarEndAlpha: CGFloat = 0
    var usingSpringWithDamping: CGFloat = 1
    var isHidden: Bool