这可能只是一个侧边栏库,但这正是我希望实现的方式。
code
使用(以下为示例)#import <JHSidebar/JHSidebarViewController.h>
从任何UIViewController
访问JHSidebarViewController
克隆存储库,并将"Classes"目录中的.h和.m文件放入您的项目中。
- (void)onClickLeft:(id)sender {
// Open left sidebar
[self.sidebarViewController showLeftSidebar:YES];
// Close left sidebar
[self.sidebarViewController showLeftSidebar:NO];
// Toggle left sidebar
[self.sidebarViewController toggleLeftSidebar];
}
- (void)onClickRight:(id)sender {
// Open right sidebar
[self.sidebarViewController showRightSidebar:YES];
// Close right sidebar
[self.sidebarViewController showRightSidebar:NO];
// Toggle right sidebar
[self.sidebarViewController toggleRightSidebar];
}
主视图、左侧边栏和右侧边栏可以通过以下默认标识符命名场景来设置。
这些是用于从Storyboard实例化这些视图控制器的默认标识符。如果您想覆盖它们或不需要,可以覆盖以下方法。
- (UIViewController *)instantiateMainViewController; // override
- (UIViewController *)instantiateLeftViewController; // override
- (UIViewController *)instantiateRightViewController; // override
要么返回一个视图控制器,要么返回null。
如果您不希望走Storyboard的路线,您可以直接在代码中初始化JHSidebarViewController,通过设置主视图控制器以及左侧和右侧侧边栏视图控制器。
AppDelegate.h
#import <UIKit/UIKit.h>
@class JHSidebarViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) JHSidebarViewController *sidebarViewController;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#import "LeftViewController.h"
#import "RightViewController.h"
#import <JHSidebar/JHSidebarViewController.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
// Initializes JHSidebarViewController
self.sidebarViewController = [[JHSidebarViewController alloc] init];
[self.sidebarViewController enableTapGesture]; // Enables tap on open sidebar to close
[self.sidebarViewController enablePanGesture]; // Enables panning to open and close sidebars
[self.sidebarViewController setSlideMainViewWithLeftSidebar:YES]; // Main view will slide with side bar instead of left side bar overlapping main view
[self.sidebarViewController setRightSidebarWidth:220.0f]; // Sets width of right sidebar
// Sets "main view"
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.sidebarViewController.mainViewController = navController;
// Sets "left sidebar"
self.sidebarViewController.leftViewController = [[LeftViewController alloc] initWithNibName:@"LeftViewController" bundle:[NSBundle mainBundle]];
// Sets "right sidebar"
self.sidebarViewController.rightViewController = [[RightViewController alloc] initWithNibName:@"RightViewController" bundle:[NSBundle mainBundle]];
self.window.rootViewController = self.sidebarViewController;
return YES;
}
@end
Josh Holtz,[email protected],@joshdholtz
JHSidebar 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。