如何从半透明平滑过渡到不透明的UINavigationBar iOS?

ale*_*mix 28 objective-c uinavigationbar ios

UINavigationBar在视图之间转换时,我遇到了重新配置iOS 7和8的问题.

我的应用程序目前包含以下UIViewController流程:

VC1 - > VC2 - > VC3

在这个流程中

  • VC1是主屏幕,不透明 UINavigationBar
  • VC2有半透明的 UINavigationBar
  • VC3又回到了不透明状态 UINavigationBar

我遇到的问题是这些视图之间的转换看起来都非常草率.首先,我尝试了以下内容:

在VC2中

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // configure appearance
    [self.navigationController.navigationBar configureTranslucentAppearance];
}
Run Code Online (Sandbox Code Playgroud)

并在VC1和VC3中

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // configure appearance
    [self.navigationController.navigationBar restoreDefaultAppearance];
}
Run Code Online (Sandbox Code Playgroud)

以下是上面列出的两个辅助函数的实现:

- (void)restoreDefaultAppearance {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    [self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor JTTextNavBar]}];
    [self setTintColor:[UIColor JTTextNavBar]];
    [self setBarTintColor:[UIColor JTBackgroundNavBarWithAlpha:1.0]];
    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self setBackgroundColor:[UIColor JTBackgroundNavBarWithAlpha:1.0]];
    [self setShadowImage:[UIImage navigationBarShadowImage]];
    [self setTranslucent:NO];
}

- (void)configureTranslucentAppearance {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self setBackgroundColor:[UIColor clearColor]];
    [self setShadowImage:[UIImage new]];
    [self setTranslucent:YES];
}
Run Code Online (Sandbox Code Playgroud)

这是处理此转换的最基本方法.它有以下视觉文物:

  • 当您从VC1 - > VC2开始转换时,导航栏变为黑色.动画正常完成 在此输入图像描述
  • 当从VC2 - > VC1开始时,导航栏会在segue有时间完成之前立即更改为应用程序默认颜色. 在此输入图像描述
  • 当从VC2 - > VC3开始时,导航栏立即从半透明变为app导航栏颜色,然后菜单项和VC主体动画显示. 在此输入图像描述
  • 当从VC3 - > VC2开始时,导航栏立即变为黑色并保持这种状态直到segue完成. 在此输入图像描述

这些过渡看起来都不好看.理想情况下,我希望视图与新的一起顺利过渡,UINavigationBar但我看到成功完成此操作的唯一方法是手动为每个xib添加一个工具栏.

有什么建议?抱歉,如果这种描述令人困惑:(

编辑:为每个列出的过渡添加了裁剪图像UINavigationBar和顶部UIViewController.

ale*_*mix 15

我终于找到了合适的解决方案!

似乎没有一种正确的方法可以从不透明平滑过渡到透明UINavigationBar但是您可以从具有可见状态栏的视图控制器平滑过渡到具有隐藏状态栏的视图控制器.

这开辟了一个可能的解决方法,其是添加在下面viewWillAppearVC2以上从:

[self.navigationController setNavigationBarHidden:YES animated:YES];
Run Code Online (Sandbox Code Playgroud)

完成后,手动将UINavigationBarxib 添加到xib并将其配置为透明(并添加所有必需UIBarButtonItem和视图).

如果一切都正常连接从VC1转换到VC2将以UINavigationBar与视图转换相同的速度隐藏,VC2将显示其嵌入UINavigationBar

注意:为了使其正常工作,您必须确保在viewWillAppear可从VC2访问的视图控制器中,通过以下方式重置UINavigationBar可见(如有必要):

[self.navigationController setNavigationBarHidden:NO animated:YES];
Run Code Online (Sandbox Code Playgroud)

TL; DR - 手动添加UINavigationBar到透明导航栏视图控制器,并在其viewWillAppear隐藏默认的一个通过setNavigationBarHidden:animated:

  • 请记住,setNavigationBarHidden:animated:在取消后滑动手势时会出错.http://stackoverflow.com/questions/19915657/navigation-stack-becomes-unusable-after-canceling-ios-7-back-swipe-gesture (2认同)

Tom*_*ift 15

您看到的黑色是UINavigationController's视图的背景颜色.最小化视图的一种方法是将该视图的背景颜色操纵为传出/传入视图控制器视图的颜色.如果您使用纯色,这很有效.另一种方法是使用在不透明导航栏后面扩展您的视图UIViewController.extendedLayoutIncludesOpaqueBars = YES;

  • 将`navigationController?.view.backgroundColor`设置为我想要的不透明导航栏的颜色对我来说是关键.这防止了出现不希望的黑色.非常感谢! (4认同)