如何在无Storyboard的Objective-C项目中集成SideMenu?
Absolutely, you can use the SideMenu library without Storyboards in Objective-C—no problem at all! Let me break down exactly how to set this up, including fixing that initialization issue you're running into.
Step 1: Configure Global SideMenu Settings
First, set up the base configuration for your side menu in your app delegate's didFinishLaunchingWithOptions method. This defines core behavior and appearance:
#import <SideMenu/SideMenu.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Initialize menu managers to nil first SideMenuManager.defaultManager.leftMenuNavigationController = nil; SideMenuManager.defaultManager.rightMenuNavigationController = nil; // Customize menu appearance and behavior SideMenuManager.defaultManager.menuWidth = 280; // Adjust to your preferred width SideMenuManager.defaultManager.presentStyle = SideMenuPresentationStyleMenuSlideIn; SideMenuManager.defaultManager.menuFadeStatusBar = NO; SideMenuManager.defaultManager.menuAnimationBackgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3]; // Your other app setup code... return YES; }
Step 2: Initialize the SideMenu Navigation Controller
Here's where you might have hit a snag: the SideMenuNavigationController doesn't expose the single-argument initWithRootViewController: to Objective-C. Instead, you need to use its designated initializer that includes the menu position (left or right):
// 1. Create your custom menu content view controller // Replace MenuViewController with your actual menu UI controller MenuViewController *menuContentVC = [[MenuViewController alloc] init]; // 2. Initialize the SideMenuNavigationController with position SideMenuNavigationController *leftMenuNav = [[SideMenuNavigationController alloc] initWithRootViewController:menuContentVC menuPosition:SideMenuPositionLeft]; // 3. Assign it to the manager so it's accessible globally SideMenuManager.defaultManager.leftMenuNavigationController = leftMenuNav;
If you need a right-side menu, just use SideMenuPositionRight and assign to SideMenuManager.defaultManager.rightMenuNavigationController instead.
Step 3: Trigger the Menu From Your Main UI
To open the menu from a button or gesture in your main view controller, use this code:
// Example: Button action to open left menu - (IBAction)openLeftMenuTapped:(id)sender { [self presentViewController:SideMenuManager.defaultManager.leftMenuNavigationController animated:YES completion:nil]; } // Optional: Add a swipe gesture to open the menu - (void)viewDidLoad { [super viewDidLoad]; UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(openLeftMenuTapped:)]; leftSwipe.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:leftSwipe]; }
Quick Note on the Initialization Issue
The reason you couldn't access initWithRootViewController: is because the Swift-based SideMenu library's SideMenuNavigationController uses a designated initializer that requires a menuPosition parameter. This translates to the two-argument method in Objective-C, which is the only valid way to instantiate it.
Bonus: Customize Menu Behavior
You can tweak more settings to match your app's needs, like:
- Disallow pushing the same view controller twice:
SideMenuManager.defaultManager.allowPushOfSameClassTwice = NO; - Set a custom animation duration:
SideMenuManager.defaultManager.menuAnimationDuration = 0.3; - Disable menu dismissal when tapping outside:
SideMenuManager.defaultManager.dismissOnMenuTap = NO;
内容的提问来源于stack exchange,提问作者zionun

