有任何想法或方法如何实现这一点吗?
我目前正在使用JASidePanels。谢谢!
// Setup view behind status bar for fading during menu drawer animations
if (OSVersionIsAtLeastiOS7()) {
self.statusBarView = [[UIView alloc] initWithFrame:[[UIApplication sharedApplication] statusBarFrame]];
[self.statusBarView setBackgroundColor:[UIColor blackColor]];
[self.statusBarView setAlpha:0.0];
[self.view addSubview:self.statusBarView];
}
// Setup drawer animations
__weak __typeof(&*self) weakSelf = self; // Capture self weakly
[self setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
MMDrawerControllerDrawerVisualStateBlock block;
block = (drawerSide == MMDrawerSideLeft) ? [MMDrawerVisualState parallaxVisualStateBlockWithParallaxFactor:15.0] : nil; // Right side animation : Left side animation
if(block){
block(drawerController, drawerSide, percentVisible);
}
[weakSelf.statusBarView setAlpha:percentVisible]; // THIS IS THE RELEVANT CODE
}];
self.statusBarView
作为私有属性。
代码的第一部分创建了一个视图,对其进行配置,并将其添加为MMDrawerController子类视图的子视图。 OSVersionIsAtLeastiOS7()
方法是一个自定义方法,简化了检查设备是否运行iOS 7的过程(如果不是,则您的自定义视图将显示在状态栏下方,您不希望这样)。
代码的第二部分是MMDrawerController的setDrawerVisualStateBlock
方法,它设置了在打开和关闭菜单时要执行的动画代码。代码的前几行是样板代码,将预构建的动画块之一设置为每个菜单(我想在左侧使用视差效果,但在右侧不使用)。相关代码是块的最后一行:[weakSelf.statusBarView setAlpha:percentVisible];
,它将状态栏视图的不透明度设置为与菜单当前打开的百分比相匹配。这允许您在Facebook应用程序中看到平滑的交叉动画。您还会注意到我将self
分配给变量weakSelf
,以避免“保留循环”编译器警告。
我一直在试图实现同样的目标。我正在使用以下概念来完成此操作:
因此,首先,您需要创建两个具有所需UINavigationBar外观的图像:
一个640x128px的图像来覆盖导航栏和状态栏(ImageA)
以及一个640x88px的图像来覆盖导航栏但将状态栏保持黑色(ImageB)。
在application:didFinishLaunchingWithOptions:
方法中,使用[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"ImageA.png"] forBarMetrics:UIBarMetricsDefault];
将UINavigationBar的背景设置为ImageA。
当侧边菜单开始打开时,您需要切换UINavigationBar以使用ImageB并创建一个视图,您将在UIStatusBar下方添加该视图。下面是一些示例代码:
// Add a property for your "temporary status bar" view
@property (nonatomic, strong) UIView *temporaryStatusBar;
在侧边菜单开始展开的代码中:
// Create a temporary status bar overlay
self.temporaryStatusBar = [[UIView alloc] initWithFrame:[[UIApplication sharedApplication] statusBarFrame]];
self.temporaryStatusBar.backgroundColor = [UIColor yourColor];
[self.navigationController.view addSubview:self.temporaryStatusBar];
// Update both the current display of the navigationBar and the default appearance values
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"imageB.png"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imageB.png"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setNeedsDisplay];
你可以使用这个很棒的侧边菜单库
https://github.com/arturdev/AMSlideMenu
在这个演示项目中,您可以通过编写4行代码来实现。- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
// 设置导航栏的色调颜色 self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:@"#365491" alpha:1];
// 制作与导航栏相同颜色的视图 UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)]; statusBarView.backgroundColor = [UIColor colorWithHex:@"#365491" alpha:1];
// 用创建的视图替换状态栏视图并进行魔法 :) [[self mainSlideMenu] fixStatusBarWithView:statusBarView]; }