簡體   English   中英

NavigationBar標題文本不會隨着titleTextAttributes更改

[英]NavigationBar title text won't change with titleTextAttributes

我有一個奇怪的問題,我不太了解。

我有兩種觀點,一種應該是黑色標題,另一種應該是白色標題。

我遇到的問題是我可以設置顏色一次,而不能將其改回。

因此,當我從帶有黑色標題的視圖轉到具有白色標題的視圖然后返回時,標題不會變回黑色。

viewWillAppear中白色標題的代碼:

self.navigationController?.navigationBar.barStyle = .black
self.navigationController?.navigationBar.tintColor = .white
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

viewWillAppear中黑色標題的代碼:

self.navigationController?.navigationBar.isHidden = false
self.navigationController?.navigationBar.barStyle = .default
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]

當我明確設置新顏色時,為什么不變回來?

編輯:添加完整的代碼

黑色標題視圖:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.isHidden = false
    self.navigationController?.navigationBar.barStyle = .default
    self.navigationController?.navigationBar.tintColor = hexStringToUIColor(hex: "4CAF50")
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
    self.navigationItem.title = listData.name

    clearStatusBarColor()

    let editButton = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(tapToEdit))
    let sortImg = UIImage(named: "sort")
    sortingButton = UIBarButtonItem(image: sortImg, style: .plain, target: self, action: #selector(tapToSort))
    self.navigationItem.rightBarButtonItems = [sortingButton!, editButton]

    self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = nil

    // get updated Data
    if User.active.hasListUpdated {
        // return with new gameEntries -> Update
        listData = User.active.allLists![listDataIndex] // To keep upToDate data!

        listEntries = listData.list_entries!

        gameEntries = listEntries.compactMap({ (entry: ListEntry) -> GameEntry in
            return GameEntry(game: entry.game, platform: nil, platform_id: entry.platform, rating: Int(entry.rating ?? 0), review: nil, notes: entry.description)
        })

        listTable.reloadData()
    }

    // Sorting
    if hasSortChanged {
        hasSortChanged = false
        sortList(sort: sortingOption, order: sortingOrder)
    }
}

白色標題視圖:

    override func viewWillAppear(_ animated: Bool) {
    if !isPreviewing {
        self.navigationController?.navigationBar.isHidden = false
        self.navigationController?.navigationBar.barStyle = .black
        self.navigationController?.navigationBar.tintColor = .white
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

        // MARK: Clear StatusBar
        clearStatusBarColor()

        if transparentNav {
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for:UIBarMetrics.default)
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.title = nil
        } else {
            self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
            self.navigationController?.navigationBar.shadowImage = nil
            self.title = game.name!
        }
    }

    // MARK: NavigationBar
    let button = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(showOptions))
    self.navigationItem.rightBarButtonItem = button


    // Check if game should have new review or rating
    if User.active.hasMainScreenUpdated {
        // Update rating
        updateUserRating()
        // update review
        updateUserReviewStatus()
    }

    // Update lists! 
    if User.active.hasListUpdated {
        updateListsStatus()
    }

}

如果要在不同的視圖控制器中更改導航欄顏色,建議您使用UIViewController的子類並通過該類處理導航欄更改。 這是您的情況的一個例子。

class CustomUIViewController: UIViewController {

    override func didMove(toParentViewController parent: UIViewController?) {
        super.didMove(toParentViewController: parent)
        if parent == nil {
            if SettingsManager.LastBarColor == .default {
                self.setLightBars()
            }
            else {
                self.setDarkBars()
            }
        }
    }

    func setDarkBars() {
        SettingsManager.LastBarColor = .lightContent
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
        tabBarController?.tabBar.tintColor = UIColor.white
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    }

    func setLightBars() {
        SettingsManager.LastBarColor = .default

        UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
        tabBarController?.tabBar.tintColor = UIColor.Black
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.Black]
        navigationController?.navigationBar.barTintColor = UIColor.white
        navigationItem.titleView?.tintColor = UIColor.Black
    }
}

class SettingsManager {

    class var LastBarColor: UIStatusBarStyle = .default

}

然后在您的視圖控制器中使用CustomUIViewController,在viewWillAppear()函數中調用setDarkBars()或setLightBars()。

您可以使用自定義UINavigationController類,然后重寫pushViewController函數來在navigationBar上設置所需的內容。

viewWillAppear方法在這里有很多代碼。

class MyNavigationViewController: UINavigationController {
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: animated)
        self.updateForVC(viewController: viewController)

    }

    func updateForVC(viewController: UIViewController) {
        //DO WHATEVER YOU WHANT HERE, title, img, etc

        var color = UIColor.black

        if viewController.isKind(of: MyClass.self) {
            color = UIColor.white
        }

        self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: color]
    }

}   

嘗試pushViewController導航,對我有用

if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController {
      if let navigator = self.navigationController {
        navigator.pushViewController(viewController, animated: true)
       viewController.title = ""
      }
    }

暫無
暫無

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

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