本文介绍了ViewWillAppear中的Navigationbar着色在iOS 10中为时太晚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的错误,该错误仅在iOS 10上发生.

我有一个具有多个屏幕的应用程序,每个屏幕为 viewWillAppear 中的 navigationBar 着色.因此,当您转到下一个屏幕时,它将正确显示颜色.

但是,在iOS 10上进行测试时,回到上一个屏幕时,我突然看到以下行为:当前一个屏幕出现时, navigationBar 仍具有前一个屏幕的颜色,然后闪烁为正确的颜色.看起来几乎像 viewWillAppear 一样表现为 viewDidAppear .

相关代码:

ViewController:

 -(void)viewWillAppear:(BOOL)动画{[super viewWillAppear:animated];[ViewControllerPainter paint:self withBackground:[UIColor whiteColor] andForeground:[UIColor blackColor] andIsLight:true];} 

画家:

  +(void)paint:(UIViewController *)controller withBackground:(UIColor *)backgroundColor andForeground:(UIColor *)foregroundColor andIsLight:(bool)isLight{controller.navigationController.navigationBar.opaque = true;controller.navigationController.navigationBar.translucent = false;controller.navigationController.navigationBar.tintColor =前景颜色;controller.navigationController.navigationBar.barTintColor = backgroundColor;controller.navigationController.navigationBar.backgroundColor = backgroundColor;controller.navigationController.navigationBar.barStyle = isLight吗?UIBarStyleDefault:UIBarStyleBlack;controller.navigationController.navigationBar.titleTextAttributes = @ {NSForegroundColorAttributeName:前景色};} 

这是一个错误吗?有什么我可以解决的办法吗?非常令人沮丧.

解决方案

根据

对我来说,快速解决方案是覆盖 popViewControllerAnimated pushViewController 并更新 UINavigationController navigationBar 背景>.看起来是这样的:

  override func popViewControllerAnimated(animated:Bool)->UIViewController?{让poppedViewController = super.popViewControllerAnimated(动画)//更新导航栏外观updateAppearanceForViewController(nextViewController)返回poppedViewController}覆盖func pushViewController(viewController:UIViewController,动画:Bool){super.pushViewController(viewController,动画:动画)//更新导航栏外观updateAppearanceForViewController(viewController)} 

我的猜测是它有效,因为 popViewControllerAnimated pushViewController 不是由于布局更改而由OS调用的,而是由touch事件调用的.因此,如果您想找到另一个地方来更新您的 navigationBar 背景,请记住这一点.

I am facing a weird bug, that happens only on iOS 10.

I have a application with several screens, and each screen colors the navigationBar in viewWillAppear. So when you go to the next screen, it will be properly colored.

However, when testing on iOS 10 I suddenly see the following behaviour when going back to a previous screen:When the previous screen appears the navigationBar still has the color of the previous screen and then flashes to the proper color.It almost looks like viewWillAppear somehow behaves as viewDidAppear.

Relevant code:

ViewController:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [ViewControllerPainter paint:self withBackground:[UIColor whiteColor] andForeground:[UIColor blackColor] andIsLight:true];

}

Painter:

+ (void)paint:(UIViewController *)controller withBackground:(UIColor *)backgroundColor andForeground:(UIColor *)foregroundColor andIsLight:(bool)isLight
{
    controller.navigationController.navigationBar.opaque = true;
    controller.navigationController.navigationBar.translucent = false;
    controller.navigationController.navigationBar.tintColor = foregroundColor;
    controller.navigationController.navigationBar.barTintColor = backgroundColor;
    controller.navigationController.navigationBar.backgroundColor = backgroundColor;
    controller.navigationController.navigationBar.barStyle = isLight ? UIBarStyleDefault : UIBarStyleBlack;
    controller.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: foregroundColor};
}

Is this a bug? Is there something I can do about to fix this? It's very frustrating.

解决方案

Here's what changed according to the iOS 10 SDK Release Notes:

So the problem seems to be that viewWillAppear is triggering the mentioned layout loop, since it's called as a result of a layout change:

The quick fix for me was overriding popViewControllerAnimated and pushViewController and updating the navigationBar background on my subclass of UINavigationController. Here's how it looks like:

override func popViewControllerAnimated(animated: Bool) -> UIViewController? {
    let poppedViewController = super.popViewControllerAnimated(animated)

    // Updates the navigation bar appearance
    updateAppearanceForViewController(nextViewController)

    return poppedViewController
}

override func pushViewController(viewController: UIViewController, animated: Bool) {
    super.pushViewController(viewController, animated: animated)

    // Updates the navigation bar appearance
    updateAppearanceForViewController(viewController)
}

My guess is that it works because popViewControllerAnimated and pushViewController are not called by the OS as a result of a layout change, but by a touch event. So keep that in mind if you want to find another place to update your navigationBar background.

这篇关于ViewWillAppear中的Navigationbar着色在iOS 10中为时太晚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 13:22