關於這個問題:How to change Back button text from within the child view controller?我正在尋找一個合適的方法來刷新導航欄後更改後退按鈕標題previousViewController.navigationItem.backBarButtonItem?.title = "New Title"
。如何正確刷新UINavigationBar?
從鏈接的問題的(不那麼理想的?)溶液:
if let navigationController = self.navigationController {
navigationController.popViewControllerAnimated(false)
navigationController.pushViewController(self, animated: false)
}
編輯:
顯然改變層幀迫使導航欄刷新。 (?)不是一個解決方案,但更便宜的解決方法我想:
if let navigationController = self.navigationController {
navigationController.navigationBar.layer.frame.insetInPlace(dx: 0.1, dy: 0)
navigationController.navigationBar.layer.frame.insetInPlace(dx: -0.1, dy: 0)
}
很多時候,當沒有一個簡單的方法來做你想做的事時,這是很好的理由。後退按鈕應將用戶帶回他們來自的地方,他們的導航歷史不應該被重寫。如果你在左上角需要一些其他的行爲(除了導航),你不需要使用'backBarButtonItem'。 – nhgrif
在某些特定情況下,更改標題可改善用戶體驗。以WhatsApp爲例。 – MJQZ1347
假設我從未使用過WhatsApp。 – nhgrif