在 iOS 7 和 8 中,我在视图之间进行转换时,重新配置 UINavigationBar
出现了问题。
我的应用程序当前包含以下 UIViewController
流程:
VC1 --> VC2 --> VC3
在这个流程中:
- VC1 是主屏幕,并且具有不透明的
UINavigationBar
- VC2 具有半透明的
UINavigationBar
- VC3 回到具有不透明的
UINavigationBar
我遇到的问题是这些视图之间的过渡看起来都非常粗糙。首先我尝试了以下方法:
在 VC2 中:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// configure appearance
[self.navigationController.navigationBar configureTranslucentAppearance];
}
并且在VC1和VC3中
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// configure appearance
[self.navigationController.navigationBar restoreDefaultAppearance];
}
这里是上述两个辅助函数的实现:
- (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];
}
这是处理此转换的最基本方法,但会产生以下视觉伪影:
- 从VC1 --> VC2 的转换开始时,导航栏立即变为黑色。然后动画正常完成
- 从VC2 --> VC1 的转换中,在segue足够完成之前,导航栏立即更改为应用程序的默认颜色。
- 从VC2 --> VC3 的转换中,导航栏立即从半透明变成应用程序导航栏颜色,然后菜单项和 VC body 开始动画。
- 从VC3 --> VC2 的转换中,导航栏立即变为黑色,并保持此状态直到segue完成。
所有这些转换看起来都不好。理想情况下,我希望视图能够与它们的新UINavigationBar
平稳过渡,但我所见过成功实现这一点的唯一方法是在每个XIB中手动添加工具栏。
有什么建议吗?如果这个描述让人困惑,我很抱歉 :(
编辑:已添加所列转换中UINavigationBar
和UIViewController
顶部的裁剪图像。
+[UIView animateWithDuration:animations:]
来实现这些变化的动画效果吗? - Palpatim[self.navigationController.navigationBar configureTranslucentAppearance];
之前将UINavigationBar backgroundColor设置为应用程序默认值。这至少不会在从不透明到半透明时显示临时黑色栏,尽管仍存在从半透明到不透明的问题。感谢您的建议。 - alexgophermixtranslucent
属性的更改。与过渡的其余部分不同,透明度似乎会立即应用。建议在viewDidAppear
中单独设置translucent
属性,这样可以延迟推送/弹出开始时看到的突然变化。特别是当viewWillAppear
将此从NO更改为YES时,将该属性更改延迟到viewDidAppear
。虽然完全透明度要延迟到设置为YES,但应该可以消除黑色条纹效果。 - Rory McKinnel