我有两个不同导航栏的视图控制器。每个导航栏都有不同的颜色。
如果我从VC1移动到VC2,我会看到不同的颜色,但如果向后移动,我会在VC1导航栏中看到VC2的颜色。
因此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:
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()
}
如何在VC1中制作固定的导航栏颜色?谢谢你的帮助!
答案 0 :(得分:1)
不要在viewDidLoad中更改导航栏的颜色,而是在viewWillAppear中执行:
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
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)
}
答案 1 :(得分:1)
如果从VC1到VC2,它们有不同的导航控制器。因此,导航条颜色应该没有任何问题。因为他们使用不同的导航。但是,如果从VC1推送到VC2,并且从VC2返回到VC1,则应使用viewWillAppear
方法设置VC1的导航栏颜色。因为当你回到VC1时,由于已经在内存中创建了VC1,因此它会继续与viewWillAppear
一起运行,而不是与viewDidLoad
一起运行。