PopViewControllerAnimated上的UINavigationBar自定义丢失了

时间:2014-08-06 08:37:56

标签: ios7 uinavigationcontroller uinavigationbar customization

我的设置

使用iOS 7,在UINavigationController中我推动3个UIViewControllers,每个人都以不同的外观自定义UINavigationBar:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self customizeNavBar];
}
- (void) customizeNavBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;

    CALayer *navBar = [CALayer layer];
    [navBar setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [navBar setFrame:CGRectMake(0, -20, 320, 64)];
    [navBar setOpacity:0.4];

    [self.navigationController.navigationBar.layer insertSublayer:navBar atIndex:0];
}

并且AppDelegate上没有任何自定义。

我的问题

推送VC时一切正常,但是当它弹出时,最后一个自定义应用了剩下的一个,我找不到再次应用相应自定义的方法。

1 个答案:

答案 0 :(得分:1)

首先,iOS 7导航栏的颜色令人头疼,从7.0版本变为7.1版本。但这是一个可能适合您的解决方案。

当您显示新VC时,您必须删除navBar CALayer,以便在弹出VC时导航栏颜色正确更新。

这个解决方案的唯一问题是在transulcent导航栏到无半透明导航栏之间的过渡期间,反之亦然,你会发现变化。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.navigationBar.translucent = NO;
    [self.navColourView.layer removeFromSuperlayer];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.navigationBar.translucent = YES;
    [self.navigationBar.layer insertSublayer:self.navColourView.layer atIndex:1];
}

以下是关于此主题的讨论https://gist.github.com/alanzeino/6619253