[英]add navigation bar but there is no back button
我創建了一個表格視圖,然后說用戶按下了一個單元格,它將轉到ListTavleView
但是現在唯一的問題是,只要用戶在ListTableView中,就沒有后退按鈕,甚至認為我已經嵌入了導航控制器
我想拳頭視圖導航欄是小標題第二視圖導航欄是大標題
下面是我的代碼
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
controller.item = items
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
以下是我的Storybord設置
沒有后退按鈕的導航欄
如果您使用pushViewController,最好只獲取另一個視圖控制器的引用,它總是一個后退按鈕,因為您要推入導航控制器,這是一個簡單的示例:
let story = UIStoryboard(name: "Main", bundle: nil)
let vc = story.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
self.navigationController?.pushViewController(vc, animated: true)
至於后退按鈕,問題出在您的層次結構上。 您是否在另一個視圖控制器中更改導航欄的左側項目,這可能會影響目標視圖控制器中的導航欄。
如果要在下一個視圖上具有導航欄后退按鈕,則只需在導航上按目標視圖,它將顯示默認的導航后退按鈕。 不,需要任何額外的工作。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
guard let controller = segue.destination as? ListTableViewController else {
return
}
controller.item = items
self.navigationController?.pushViewController(controller, animated: true)
}
}}
而且,如果您使用segue推送viewcontroller,則無需在以下行添加self.navigationController?.pushViewController(controller, animated: true)
您正在將新的NavigationController(例如Nav.B)推送到現有的(Nav.A)。
每個導航控制器保留不同的導航堆棧。 將viewcontroller添加到Navigation controller時,后退按鈕可見。 閱讀有關UINavigationController的更多信息。
對於您當前的情況,您可以刪除第二個導航控制器(我認為這不是必需的)並將直接ListTableViewController
連接到ListTableViewController
所以這
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
變成
let controller = segue.destination as! ListTableViewController
當您需要大標題時(可用11個以上),可以在viewDidLoad()
添加此行
navigationController?.navigationBar.prefersLargeTitles = true
如果只需要此Viewcontroller,則添加viewWillDisappear()
或viewDidDisappear()
navigationController?.navigationBar.prefersLargeTitles = false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.