最简单的 iOS 侧边栏菜单实现。干净、简单,从左或右显示侧边栏面板,支持旋转,可以提供基本的自定义动画,并且您可以完全控制侧边栏和中心视图以操纵其透明度和内容,但是对于视图的框架除外,因为我们必须修改这些以适应 UISidebarViewController 并对侧边栏进行动画处理。
支持 iOS 6 和 7。注意:侧边栏显示在主中心视图上方。
关键词:Xcode、ios、侧边栏、菜单、汉堡、面板、简单。
超级简单的设置!
#import "UISidebarViewController.h"
,通常在 AppDelegate 中。加分项:自定义侧边栏,观察侧边栏显示/隐藏通知,或设置自定义显示/关闭动画。
#import "UISidebarViewController.h"`
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create base view controller
UIViewController *rootVC = [[UIViewController alloc] initWithNibName:@"RootView" bundle:nil];
// Create menu sidebar controller
UITableViewController *menuVC = [[UITableViewController alloc] initWithNibName:@"MenuView" bundle:nil];
self.viewController = [[UISidebarViewController alloc]
initWithCenterViewController:rootVC
andSidebarViewController:menuVC];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
您可以配置的额外属性
/** Direction in which the sidebar should come from, defaults to left */
@property (nonatomic, assign) UISidebarViewControllerDirection direction;
/** Duration of slide animation when displaySidebar is called, defaults to 0.2 */
@property (nonatomic, assign) CGFloat animationDuration;
/** Width for sidebar to slide to, defaults to 270 */
@property (nonatomic, assign) CGFloat sidebarWidth;
/** Opacity of the black overlay on center view when sidebar is out, defaults to 0.5k */
@property (nonatomic, assign) CGFloat overlayOpacity;
您可以检查的属性
@property (nonatomic, assign, readonly) BOOL sidebarIsShowing;
用于查看侧边栏是否正在显示或正在显示过程中。@property (nonatomic, strong, readonly) UIViewController *centerVC;
直接引用中心视图控制器。@property (nonatomic, strong, readonly) UIViewController *sidebarVC;
直接引用侧边栏视图控制器。当侧边栏即将显示或完成后会触发四个通知。这些通知通过默认的 NSNotificationCenter 发布,名称为
UISidebarViewControllerNotificationDidShow
UISidebarViewControllerNotificationDidHide
UISidebarViewControllerNotificationWillShow
UISidebarViewControllerNotificationWillHide
这些通知在动画显示/隐藏侧边栏之前调用。这意味着如果在侧边栏被拖动时发送通知,则不会发布。为了观察这些通知
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(notificationHandler:)
name:@"UISidebarViewControllerNotificationWillShow"
object:nil];
侧边栏显示/隐藏的默认动画只是一个简单的水平滑入。您也可以通过设置以下四个自定义动画。
@property (nonatomic, copy) AnimationBlock showSidebarAnimation;
@property (nonatomic, copy) AnimationCompletionBlock showSidebarCompletion;
@property (nonatomic, copy) AnimationBlock hideSidebarAnimation;
@property (nonatomic, copy) AnimationCompletionBlock hideSidebarCompletion;
AnimationBlock
和 AnimationCompletionBlock
定义如下
/** Custom animation block type, targetFrame is calculated target frame location for sidebar */
typedef void (^AnimationBlock)(CGRect targetFrame);
/** Custom completion block type, finished refers to whether or not the animation was completed */
typedef void (^AnimationCompletionBlock)(BOOL finished);
MIT