iOS Objective-C添加右侧侧边栏求助:SWRevealViewController的rightRevealToggle失效
Hey there! I’ve dealt with this exact frustration with SWRevealViewController before—left menu works like a charm, but the right one just won’t cooperate. Let’s break down the most common fixes that usually get it up and running:
1. Verify the Right View Controller is Properly Assigned
First things first: make sure you’ve actually set the rightViewController property on your SWRevealViewController instance. This is easy to overlook!
Code-based setup:
// In your front view controller (the main content screen) if let revealVC = self.revealViewController() { let rightSidebarVC = YourRightSidebarViewController() // Replace with your VC class revealVC.rightViewController = rightSidebarVC // Optional: Set the reveal width for the right sidebar revealVC.rightViewRevealWidth = 280 // Adjust to your preferred width }
Storyboard-based setup:
- Ensure you’ve created a segue from your SWRevealViewController to your right sidebar VC, and set the segue’s identifier to
sw_right(this is the default identifier SWRevealVC looks for right sidebars). - Double-check that the segue’s "Kind" is set to "Custom" and the class is
SWRevealViewControllerSegueSetController.
2. Correctly Bind the rightRevealToggle Action
A common mistake is misbinding the button action to the wrong method or target. Make sure your right menu button is hooked up properly:
// In your front view controller's viewDidLoad() yourRightMenuButton.addTarget( self.revealViewController(), action: #selector(SWRevealViewController.rightRevealToggle(_:)), for: .touchUpInside )
- If you’re using Objective-C, the selector would be
@selector(rightRevealToggle:). - Also confirm that
self.revealViewController()isn’t returningnil—if it is, your front view controller isn’t properly embedded in the SWRevealViewController hierarchy.
3. Enable Right-Side Gestures (If Needed)
If you want swipe gestures to open/close the right sidebar, you need to explicitly enable them:
if let revealVC = self.revealViewController() { // Enable right-to-left swipe to open right sidebar revealVC.rightPanGestureRecognizer().isEnabled = true // Optional: Enable tap to close (if you want that behavior) revealVC.tapGestureRecognizer().isEnabled = true }
4. Check for Version Compatibility
Older versions of SWRevealViewController might have different API for right sidebars. If you’re using a legacy version:
- Confirm that
rightRevealToggleexists (some older versions usedrevealTogglewith a direction parameter). - Consider updating to the latest stable version of the library (just make sure to test for breaking changes first).
5. Debug the View Controller Hierarchy
If none of the above works, print out your view controller hierarchy to ensure everything is nested correctly:
print(self.revealViewController() ?? "Reveal VC is nil") print(self.revealViewController()?.rightViewController ?? "Right VC is nil")
If either is nil, you’ll need to fix how your SWRevealViewController is initialized or how your front view controller is presented.
Let me know if any of these steps resolve your issue, or if you can share more details (like code snippets or storyboard setup) and I can help dig deeper!
内容的提问来源于stack exchange,提问作者Charishma Rao

