gpt4 book ai didi

ios10 - ViewWillAppear中的Navigationbar着色在iOS 10中为时已晚

转载 作者:行者123 更新时间:2023-12-03 12:27:28 25 4
gpt4 key购买 nike

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

我有一个具有多个屏幕的应用程序,每个屏幕在navigationBar中为viewWillAppear上色。因此,当您进入下一个屏幕时,它将正确着色。

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

相关代码:

ViewController:

- (void)viewWillAppear:(BOOL)animated
{
[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 = foregroundColor;
controller.navigationController.navigationBar.barTintColor = backgroundColor;
controller.navigationController.navigationBar.backgroundColor = backgroundColor;
controller.navigationController.navigationBar.barStyle = isLight ? UIBarStyleDefault : UIBarStyleBlack;
controller.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: foregroundColor};
}

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

最佳答案

这是根据iOS 10 SDK Release Notes更改的内容:

In iOS 10, UIKit has updated and unified background management for UINavigationBar, UITabBar, and UIToolbar. In particular, changes to background properties of these views (such as background or shadow images, or setting the bar style) may kick off a layout pass for the bar to resolve the new background appearance.
In particular, this means that attempts to change the background appearance of these bars inside of -[UIView layoutSubviews], -[UIView updateConstraints], -[UIViewController willLayoutSubviews], -[UIViewController didLayoutSubviews], - [UIViewController updateViewConstraints], or any other method that is called in response to layout may result in a layout loop.



因此问题似乎是 viewWillAppear触发了提到的布局循环,因为它是由于布局更改而被调用的:
viewWillAppear stack trace

对我来说,快速解决方案是覆盖 popViewControllerAnimatedpushViewController并更新我的 navigationBar子类上的 UINavigationController背景。看起来是这样的:
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)
}

我的猜测是可行的,因为OS并不是由于布局更改而通过触摸事件来调用 popViewControllerAnimatedpushViewController的。因此,如果您想找到另一个地方来更新 navigationBar背景,请记住这一点。

关于ios10 - ViewWillAppear中的Navigationbar着色在iOS 10中为时已晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39511088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com