syedfa syedfa - 1 year ago 93
iOS Question

Trying to make rightBarButton appear after making it disappear using Swift

I currently am working on an app in Swift where in my viewDidLoad() method I have purposely hidden my rightBarButton on my navigation bar like this:

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(TableViewController.dismiss))
self.navigationItem.setRightBarButtonItem(nil, animated: true)

However, under certain circumstances, I would like to display the rightBarButton. How would I do this? What would be the opposite of the above line of code? Just wondering.

Thanks in advance to all who reply.

Answer Source

Once you set the bar button item to nil, it is gone. Something you can do however, is store the bar button item like so:

let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(TableViewController.dismiss));

and then you can make it appear/disappear like so:

self.navigationItem.rightBarButtonItem = barButtonItem
self.navigationItem.setRightBarButtonItem(nil, animated: true)

then just access the barButtonItem whenever you want it to appear/disappear.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download