NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: Project 7 Filter challenge - how to "refresh" navigation bar?

Forums > 100 Days of Swift


Hello, I was facing the challenge on the Project 7, the one with filtering petitions and i thought it'd be cool to put a "home" button on the navigation bar, that put all petitions in the table view after they were filtered. It's working fine if the button sits there all the time - I just add this to navigationItem.leftBarButtonItems array after view is loaded. Although, when all the petitions are already shown, there no point for this button to exist. It could be added to the leftBarButtonItems array during the app is running, but it doesn't appear there just like that - is there any method to reload navigation bar?

Here's my viewDidLoad method with the solution I tried (didn't work):

override func viewDidLoad() {
        let urlString: String

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(showCredits))
        let home = UIBarButtonItem(title: "Home", style: .plain, target: self, action: #selector(loadHomeView))
        let filter = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(filterPetitions))
        navigationItem.leftBarButtonItems = [filter]

        if navigationController?.tabBarItem.tag == 0 {
            urlString = ""
        } else {
            urlString = ""

        if let url = URL(string: urlString) {
            if let data = try? Data(contentsOf: url) {
                parse(json: data)

        if petitions.count != originalPetitions.count {
            navigationItem.leftBarButtonItems?.insert(home, at: 0)

There are two arrays of petitions, since I decided to store the data from JSON as the "originalPetitions" and "petitions" as the array to show in tableview. The "loadHomeView" method assign originalPetitions to petitions and reload tableview, the "filterPetitions" assign filtered petitions to petitions array - it's working fine.


I did something similar, but I replaced the filter button with a button to clear the filter whenever the filter was active. Then in the code where the filter gets cleared, I just set it back.

So i my viewDidLoad I initialized two view controller properties and then set the navigation bar button to the filter icon like so:

filterButton = UIBarButtonItem(image: UIImage(systemName: ""), style: .plain, target: self, action: #selector(filterPetitions))
clearFilterButton = UIBarButtonItem(image: UIImage(systemName: "xmark"), style: .plain, target: self, action: #selector(clearFilter))

navigationItem.leftBarButtonItem = filterButton

Then in my filterPetitions method, I set navigationItem.leftBarButtonItem = clearFilterButton and in clearFilter I set it back to filterButton.



Thanks for your respond! It works great - in my case I change the navigationBar.leftBarButtonItems array from [filter] to [home, filter] and backwards :D


Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.