簡體   English   中英

導航欄顏色未保存

[英]Navigation Bar color is not saving

我有帶有兩個不同導航欄的視圖控制器。 每個導航欄都有不同的顏色。

VC1 和 VC2

如果我從 VC1 移動到 VC2,我會看到不同的顏色,但如果向后移動,我會看到 VC1 導航欄的顏色來自 VC2。

視圖控制器 1 返回

所以來自 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中制作固定導航欄顏色? 謝謝你的幫助!

不要在 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)
    }

如果你從 VC1 到 VC2,它們有不同的導航控制器。 因此,導航欄顏色應該沒有任何問題。 因為他們使用不同的導航。 但是,如果您從 VC1 推送到 VC2 並且當您從 VC2 移回 VC1 時,您應該在viewWillAppear方法中設置 VC1 的導航欄顏色。 因為當您移回 VC1 時,它會繼續使用viewWillAppear運行,而不是使用viewDidLoad運行,因為 VC1 已經在內存中創建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM