简体   繁体   English

导航栏颜色未保存

[英]Navigation Bar color is not saving

I have view controllers with two different navigation bars.我有带有两个不同导航栏的视图控制器。 Every navigation bar has different color.每个导航栏都有不同的颜色。

VC1 and VC2 VC1 和 VC2

If I move from VC1 to VC2 I will see different colors but if move back I will see in VC1 navigation bar's color from VC2.如果我从 VC1 移动到 VC2,我会看到不同的颜色,但如果向后移动,我会看到 VC1 导航栏的颜色来自 VC2。

View Controller 1 Returned视图控制器 1 返回

So navigation bar color from VC1 is not saving properly所以来自 VC1 的导航栏颜色没有正确保存

VC1: VC1:

import UIKit

class TableViewController_1: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 56.0/255.0, green: 208.0/255.0, blue: 125.0/255.0, alpha: 1.00)

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

VC2: VC2:

import UIKit

class TableViewController_2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
         self.navigationController?.navigationBar.barTintColor = UIColor(red: 105.0/255.0, green: 28.0/255.0, blue: 56.0/255.0, alpha: 1.00)

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

How to make fixed navigation bar color in VC1?如何在VC1中制作固定导航栏颜色? Thank you for help!谢谢你的帮助!

Instead of changing the color of the navigation bar in the viewDidLoad, do it in viewWillAppear:不要在 viewDidLoad 中更改导航栏的颜色,而是在 viewWillAppear 中进行:

VC1 VC1

override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 56.0/255.0, green: 208.0/255.0, blue: 125.0/255.0, alpha: 1.00)
    }

VC2 VC2

override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 105.0/255.0, green: 28.0/255.0, blue: 56.0/255.0, alpha: 1.00)
    }

If you present from VC1 to VC2, they have different navigation controller.如果你从 VC1 到 VC2,它们有不同的导航控制器。 So, there should not be any problem about navigation bars color.因此,导航栏颜色应该没有任何问题。 Because they use different navigations.因为他们使用不同的导航。 However If you push from VC1 to VC2 and when you move back from VC2 to VC1, you should set VC1's navigation bar color in viewWillAppear method.但是,如果您从 VC1 推送到 VC2 并且当您从 VC2 移回 VC1 时,您应该在viewWillAppear方法中设置 VC1 的导航栏颜色。 Because when you move back to VC1, it continuous to run with viewWillAppear , not with viewDidLoad due to VC1 already created in memory.因为当您移回 VC1 时,它会继续使用viewWillAppear运行,而不是使用viewDidLoad运行,因为 VC1 已经在内存中创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM