UINavigationBar 颜色意外变化

发布于 2024-08-15 16:11:57 字数 608 浏览 5 评论 0原文

在我当前的 UINavigationController 中,viewControllers 堆栈如下所示。 1-> 2-> 3. 然而,在从第二个控制器转换到第三个控制器之前,我先弹出第二个控制器,这样当弹出第三个控制器时,第一个控制器位于顶部。

[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:controller animated:YES ] ;

但不知何故,这样做会将导航栏的颜色从原来的蓝色默认更改为黑色样式。在新控制器的 viewWillAppear 或 viewWillDisappear 中将 barStyle 显式设置为 UIBarStyleDefault 没有任何效果。更奇怪的是,如果我设置了 barStyle,即使是 1 -> 2 过渡将导致条形颜色变为黑色。

在写完这篇文章时,我意识到以下代码修复了这个问题。

navBar.barStyle = UIBarStyleDefault-1;

为什么我需要从默认颜色中减去 1 才能真正获得默认颜色?

In my current UINavigationController, the viewControllers stack looks like this.
1 -> 2 -> 3. Before transitioning from the 2nd to the 3rd controller however, I pop the 2nd one first so that when the 3rd controller is popped, the 1st controller is on top.

[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:controller animated:YES ] ;

But somehow doing this changes the navigationBar's color from the original blue default to the black style. Explicitly setting the barStyle to UIBarStyleDefault in the new controller's viewWillAppear or viewWillDisappear has no effect. What's stranger, if I set the barStyle at all, even the 1 -> 2 transition will cause the bar color to change to black.

By the end of writing this post, I realized that the following code fixed it.

navBar.barStyle = UIBarStyleDefault-1;

Why would I need to subtract 1 from the default to actually obtain the default color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩饰不了的爱 2024-08-22 16:11:57

我也面临同样的问题。我没有正式的答案,但我用我的方式解决了它:

-(void)viewWillAppear:(BOOL)animated { 
UIColor *navColor = [[UIColor alloc]initWithRed:0.742778 green:0.0181208 blue:0.0271147 alpha:1];
[self.navigationController.navigationBar setTintColor:navColor];}

我希望有人能提出更好的答案。

I faced the same problem as well. I don't have a formal answer but I fixed it in my way:

-(void)viewWillAppear:(BOOL)animated { 
UIColor *navColor = [[UIColor alloc]initWithRed:0.742778 green:0.0181208 blue:0.0271147 alpha:1];
[self.navigationController.navigationBar setTintColor:navColor];}

I wish someone could propose a better answer.

最终幸福 2024-08-22 16:11:57

我不会弹出第二个,只推第三个。当您取消第三次使用时,

[self.parentViewController.parentViewController popViewControllerAnimated:YES];

This for me 会返回并清理堆栈并将我留在控制器 1 上,而不显示具有所有正常属性的控制器 2。

I would not pop the second, just push the 3rd. And when you dismiss the 3rd use

[self.parentViewController.parentViewController popViewControllerAnimated:YES];

This for me reaches back and cleans up the stack and leaves me on controller 1 without showing controller 2, with all normal attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文