gpt4 book ai didi

ios - viewWillAppear 中的 UINavigationController Bar 颜色更新可见后更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:23 26 4
gpt4 key购买 nike

在我正在开发的应用程序中,我有一个 UITabBarController,其中包含每个 View 都在 UINavigationController 中。

其中一个 View 是设置屏幕,用户可以在其中更改应用程序的配色方案。当他们这样做并切换到另一个屏幕时,除了 UINavigationController 的背景之外,每个应该改变颜色的组件都已经改变了。它会在可见后几分之一秒内更新,因此会出现恼人的闪烁。

这是我的 viewWillAppear 的简化(我刚刚测试以确保它仍然会导致错误)

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

UIColor *foreGround = [self.settings getForegroundColor];
UIColor *backGround = [self.settings getBackgroundColor];

self.navigationController.navigationBar.barTintColor = backGround;
self.navigationController.tabBarController.tabBar.barTintColor = backGround;
self.view.backgroundColor = backGround;

self.navigationController.navigationBar.tintColor = foreGround;
self.navigationController.tabBarController.tabBar.tintColor = foreGround;
self.view.tintColor = foreGround;
}

更奇怪的是,我在 UINavigationController 栏中有一个 UIBarButtonItem,它的颜色在 View 可见之前已经更新,但后面的背景它仍然需要更新。

我尝试过的事情:

  • [self.view setNeedsDisplay];
  • [self.navigationController.navigationBar setNeedsDisplay];
  • viewWillLayoutSubviews
  • 执行当用户更改颜色设置时发送到此 viewController 的 NSNotification 中的任何一个(在他们单击选项卡栏以查看有问题的 viewController 之前很久)。

有没有办法强制它从屏幕上画出来,或者这样就不会发生这种闪烁?我不明白为什么它在可见时坚持更新,当我把它放在主 viewController 的 viewWIllAppear 中时。

当 UINavigationController 嵌入到标签栏 Controller 中时,有人知道如何在出现在屏幕上之前更新 UINavigationController 的栏吗?

最佳答案

您需要使用 UINavigationControllerDelegate。这是我的应用程序中的一个 Swift 代码片段,它应该可以工作:

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.delegate = self
reloadNav()
}

func reloadNav() {
//Put custom code to change UINavigationController however you want.

//Animation is not necessary, but it is nice.
UIView.animate(withDuration: 0.35) {
self.navigationController?.navigationBar.barTintColor = nil

//This is the default iOS tint color
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.2901960784, green: 0.5019607843, blue: 0.9019607843, alpha: 1)

self.navigationController?.navigationBar.barStyle = .default
}
}
}

extension ViewController: UINavigationControllerDelegate {

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

if let vc2 = toVC as? ViewController {
vc2.reloadNav()
}

return nil
}
}

TL;DR 使用func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?UINavigationControllerDelegate 通过访问 View Controller 中的重新加载函数来执行自定义动画。

希望这对您有所帮助,如果您需要更多说明/帮助,请告诉我。祝你好运!

关于ios - viewWillAppear 中的 UINavigationController Bar 颜色更新可见后更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23274417/

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