NavigationBar延迟更新barTintColor iOS10 [英] NavigationBar delay updating barTintColor iOS10

查看:97
本文介绍了NavigationBar延迟更新barTintColor iOS10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

假设我们有一个NavigationController和2个viewControllers. ViewControllerA有一个蓝色的NavigationBar,而ViewControllerB有一个绿色的.

Let's say we have one NavigationController and 2 viewControllers. ViewControllerA has a blue navigationBar, while ViewControllerB has a green one.

我像这样设置它们:

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

当我从A到B时,它工作得很好,但是当我返回时,之后会更新navigationBar teint.就像是在viewDidAppear中设置的一样.

It works well when I got from A to B, but when I return, the navigationBar teint is updated after. Like if it was set up in the viewDidAppear.

预期:

导航栏应立即更新其导航栏颜色.

The navigation bar should have its navigation bar color updated without delay.

尝试的解决方案:

  • 我已经看到了这个 SO 发布,然后我尝试了解决方案.它可以工作,但是会使导航栏的管理更加复杂和痛苦. (在真实应用中)

  • I've seen this SO post, and I tried the solution. It worked, but would make the management of the navigation bar a lot more complex and painful. (in a real app)

我已尝试在ViewWillDisappear方法中更改导航条.没用.

I've tried to change the navbar teint in the ViewWillDisappear method. Didn't work.

更多:

这是由于ios10 API的更改引起的.我已经阅读了发行说明 ,但目前尚不清楚在这种情况下应该怎么做.

This is caused by a change on the ios10 API. I've read the release notes, but it's still unclear what should be done in this case.

在iOS 10中,UIKit已针对以下内容更新并统一了后台管理: UINavigationBar,UITabBar和UIToolbar.特别是对 这些视图的背景属性(例如背景或阴影) 图像或设置条形样式)可能会为 条以解决新的背景外观.

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.

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

In particular, this means that attempts to change the background appearance of these bars inside of layoutSubviews, -[UIView updateConstraints], viewWillLayoutSubviews, viewDidLayoutSubviews, updateViewConstraints, or any other method that is called in response to layout may result in a layout loop.

在某些情况下,可以通过确保您可以中断这些布局循环 当对象(例如UIImage或 UIColor)是必需的.但通常,您应该避免这样做.

In some cases you can break these layout loops by ensuring that you always use the same object instance when objects (such as UIImage or UIColor) are required. But in general you should avoid doing this.

问题:

在iOS 10中,不同导航控制器之间处理导航栏更改的最佳方法是什么?

What would be the best way to handle the navigation bar changes between different navigation controllers in iOS 10 ?

推荐答案

尝试以下代码:

注意:代码已在Swift 3中进行了测试.

Note: Code Tested in Swift 3.

在ViewController A中:

In 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中:

In 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中:

In ViewController A:

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

在ViewController B中:

In ViewController B:

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

输出:

这篇关于NavigationBar延迟更新barTintColor iOS10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆