【问题标题】:NavigationBar delay updating barTintColor iOS10导航栏延迟更新 barTintColor iOS 10
【发布时间】:2017-02-11 14:57:30
【问题描述】:

上下文:

假设我们有一个NavigationController 和两个视图控制器。 ViewControllerA 有一个蓝色的导航栏,而ViewControllerB 有一个绿色的导航栏。

我是这样设置的:

override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.barTintColor = UIColor.blue    // Green if ViewController B
    }

当我从 A 到 B 时效果很好,但是当我返回时,navigationBar teint 会在之后更新。就像在viewDidAppear 中设置的一样。

预期:

导航栏的导航栏颜色应立即更新。

尝试过的解决方案:

  • 我看到了这个SO 帖子,并尝试了解决方案。它有效,但会使导航栏的管理更加复杂和痛苦。 (在真实应用中)

  • 我已尝试更改 ViewWillDisappear 方法中的导航栏 teint。没用。

更多:

这是由 ios10 API 的更改引起的。我已经阅读了release notes,但目前还不清楚在这种情况下应该怎么做。

在 iOS 10 中,UIKit 更新并统一了后台管理 UINavigationBar、UITabBar 和 UIToolbar。特别是,更改为 这些视图的背景属性(例如背景或阴影 图像,或设置栏样式)可能会启动布局传递 栏解决新的背景外观。

特别是,这 表示尝试更改这些条的背景外观 layoutSubviews 内部,-[UIView updateConstraints], viewWillLayoutSubviews, viewDidLayoutSubviews, updateViewConstraints, 或响应布局调用的任何其他方法可能会导致 布局循环。

在某些情况下,您可以通过确保 当对象(例如 UIImage 或 UIColor)是必需的。但总的来说,您应该避免这样做。

问题:

在 iOS 10 中处理不同导航控制器之间导航栏变化的最佳方式是什么?

【问题讨论】:

  • 你找到解决办法了吗?
  • @Joe 我最终使用了我帖子中链接的解决方案。不过,我相信它会使导航栏的管理更加复杂
  • 我做了一个与你的问题类似的测试项目。在那个项目中,我有一个没有 segue 的 3vc 连接,并且有不同的导航栏和状态栏颜色。过渡时我没有看到导航栏颜色有任何延迟。让我知道这是你之后的答案...
  • @Joe,您是否使用navigationController?因为我做了一个测试项目(如问题中所述),我可以重现同样的事情。但是使用 segues ..
  • 是的,要实现这一点。您必须调整导航栏设置并以编程方式放置状态栏...我将尝试在今天的某个时间将项目上传到 GitHub....

标签: objective-c swift uinavigationbar ios10


【解决方案1】:

试试这个代码:

注意:代码在 Swift 3 中测试。

在 ViewController A 中:

    override var isViewLoaded: Bool {
    title = "View 1"
    navigationController?.navigationBar.barTintColor = .blue
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    UIApplication.shared.statusBarStyle = .lightContent
    self.navigationController?.navigationBar.barStyle = .black // In case statusbar Light content wont work.
    return true
    }

在 ViewController B 中:

    override var isViewLoaded: Bool {
    title = "View 2"
    navigationController?.navigationBar.barTintColor = .green
    navigationController?.navigationBar.tintColor = .white
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    return true
    }

输出:没有任何延迟......

图案图像作为 TintColor。

在 ViewController A 中:

 navigationController?.navigationBar.barTintColor = UIColor(patternImage: UIImage(named: "viewOneImage.jpg")!)

在 ViewController B 中:

 navigationController?.navigationBar.barTintColor = UIColor(patternImage: UIImage(named: "viewTwoImage.jpg")!)

输出:

【讨论】:

  • 它不能解决我的问题..我在导航回上一个视图控制器时仍然遇到延迟...
  • 你可能有两个不同的导航控制器。
  • 感谢它的工作!不要忘记 super.isViewLoaded
  • 它可以工作,但最后返回 super.isViewLoaded 否则应用程序将崩溃
【解决方案2】:

我对覆盖isViewLoaded 的解决方案不满意,因为如果曾经显示view_controller_a 并且您出于任何原因使用view_controller_b.isViewLoaded 的值,那么您最终会将导航栏更改为显示错误。

相反,我会将您想要的导航栏配置存储在每个视图控制器中并使用UINavigationControllerDelegate

class NavBarConfig
{
    var background_color: UIColor

    init( background_color: UIColor )
    {
        self.background_color = background_color
    }
}

class ViewController: UIViewController
{
    var nav_bar_config: NavBarConfig?
}

class NavControllerDelegate: UINavigationControllerDelegate
{
    //Called just before the navigation controller displays a view controller’s view and navigation item properties.
    func navigationController( _ nav_controller: UINavigationController, willShow view_controller: UIViewController, animated: Bool )
    {
        if let view_controller = view_controller as? ViewController, let nar_bar_config = view_controller.nav_bar_config
        {
            nav_controller.navigationBar.barTintColor = view_controller.nar_bar_config.background_color
        }
    }
}

在导航控制器your_nav_controller.delegate = NavControllerDelegate() 上设置委托后,将在显示每个视图控制器之前调用委托方法,允许您根据需要配置导航栏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多