删除后栏按钮标题Swift

时间:2018-12-22 11:15:25

标签: ios swift uinavigationbar

我要删除后退栏按钮标题。但是我的代码无法正常工作。

class SearchFoodsTableViewController: UITableViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    var searchController: UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController:  nil)

        searchController.searchResultsUpdater = self
        searchController.delegate = self
        searchController.searchBar.delegate = self

        searchController.hidesNavigationBarDuringPresentation = false
        searchController.dimsBackgroundDuringPresentation = true

        navigationItem.titleView = searchController.searchBar
        //it is not working
        navigationItem.backBarButtonItem = nil//UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

        definesPresentationContext = true
    }

结果看起来像this。 如何正确删除标题?我知道主控制器中的这个技巧:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationItem.title = "Категории"
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)

    navigationItem.title = ""
}

它可以正常工作,但是当我回到主控制器中时,标题“Категории”的显示非常缓慢,并且该解决方案不适合我。

1 个答案:

答案 0 :(得分:1)

您必须为上一个视图控制器设置backBarButtonItem

class FirstVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "FirstVC"
        navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: .plain, target: nil, action: nil)
    }
}

class SecondVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "SecondVC"
    }
}