How can I hide or add a BarButtonItem dynamically when I change the current ViewController? - swift

I want to add a BarButtonItem every time the user jumps to the Controller and remove it dynamically when the user selects another ViewController in the navigation bar.
let filterBtn = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(action(sender:)))
navigationItem.rightBarButtonItem = filterBtn
}
I added two BarButtonItem in my UIStoryBoard and now I want to add one more button dynamically but only if the ViewController is selected like shown above.
It it possible to do it like this? I appreciate any input!

So, navigationItem.rightBarButtonItems is an array and you can remove/append elements. I'll suggest you to try removing "humburger" button item and adding the new one when DocumentationViewController is presented.
navigationItem.rightBarButtonItems?.popLast()
navigationItem.rightBarButtonItems?.append(filterBtn)

Related

how does instagram's custom navigation bar created?

I'm working on an iOS app and gonna work on the navigation controller part.
The UI of the navigation bar is not complex in my app; basically, it should have an app logo on the left an app name in the middle, and two bar button items on the right.
While I was researching, I saw the Instagram's navigation is sort of similar to what I want to try, especially the "Instagram" logo on the left.
I'm not sure if I can make a navigation bar like an instagram's navbar with a default UI navigation bar, so I guess I need to make a custom navigation bar with .xib file or something? or is it possible to make it with the default navigation bar and some codes??
If you can walk through the approach you take, please let me know...
This can be easily done with a default UINavigationController. For the logo you can create a UIBarButtonItem and set the image to whatever image you want (e.g a logo).
I have a function that sets the collection view's offset to 0 when tapping on the logo similar to Instagram.
For the 3 buttons on the right, you can do something like this:
self.navigationItem.rightBarButtonItems = [button1, button2, button3]
Each button would be their own UIBarButtonItem and then add a selector to each of them.
Here is something similar to what I have in my app:
let calendarButtonItem = UIBarButtonItem(
image: UIImage(
systemName: "calendar.badge"),
style: .done,
target: self,
action: #selector(handleCalendarTap))
let navLogoButtonItem = UIBarButtonItem(
image: UIImage(named: "nav-logo"),
style: .plain,
target: self,
action: #selector(handleNavLogoTap))
self.navigationItem.rightBarButtonItem = calendarButtonItem
self.navigationItem.leftBarButtonItem = logo

Using UINavigationItem on UITabBarController

I have a project with a UITabBarController and a couple of views. Sort of like this:
In this project, the tab bar controller is called by tapping the Tab button on the UIViewController. I would like to get rid of the back button with "Title", and replace it with an "X" icon. When tapped, the "X" icon would dismiss the UITabBarController and return to the caller. I do this all of the time on UINavigationController using a UINavigationItem, but that does not appear to work in this situation. I drag the UINavigationItem to the view and it allows it, but it does not show up on the view and any UIBarButtonItem that I drag and drop on it do not appear.
Is there a way to actually do this? I'd even be ok with leaving the existing back button as it is and just getting rid of "Title"
I figured it out right after posting the question. Just a bit more research is all it took.
To fix this, add the following to the UITabBarController code.
override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: "back")
let leftButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(dismissTabBar))
leftButton.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftButton
}
#IBAction func dismissTabBar() {
self.navigationController?.popToRootViewController(animated: true)
}
This gives me a black back button with my image. When I tap it, it brings me back to the calling 'UIViewController.

Left Bar Button Item - show name of previous View Controller

If you have a Navigation Controller with no Bar Button Items, a navigation back button will be shown with the name of the last View Controller.
I want to keep that name, as in I don't want to have to hardcode it. I do know how to add it in but I don't want to have to do that because that leaves more room for bugs.
Is there a way that I can have a left Bar Button Item and for the default one to not go away?
Add this in viewController where you want to have default back button and custom bar button item. You can customise the bar button item.
override func viewDidLoad() {
super.viewDidLoad()
let newBtn = UIBarButtonItem(title: "new", style: .plain, target: self, action: #selector(anotherMethod))
self.navigationItem.leftItemsSupplementBackButton = true
self.navigationItem.leftBarButtonItem = newBtn//self.navigationItem.leftBarButtonItems = [newBtn,anotherBtn]
}

How do I get back button effect (<)

I am writing an app , where I want custom back effect of the default OS. Like the system which is giving with Settings ( <)
< Icon
Refer this image (I want the effect like Settings with the <)
I already have the functionality, I just want the look and feel.
Effect what I am getting.
I have tried the following
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
Also, I have tried adding it via Storyboard. How do I get it ?
Use the backBarButtonItem and you will get right effect.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
Also, you make sure
self.navigationItem.backBarButtonItem will be shown only after your have pushed another one view to navigation stack, controlled by self.navigationController, if no left button on navigation bar is displayed.

Swift - Overwrite navigation bar

When it comes to opening a new View Controller i didnt want it to be presented modally so i pushed it in navigation controller:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("newView")
self.navigationController!.pushViewController(vc, animated: true)
The View with the identifier "newView" is presented correctly and there is also a navigation bar with a back-button provided, which also works properly.
But how to customize this existing navigation bar, add new bar button items etc.. ?
any suggestions?
Thanks and Greetings!
You are on the right track by subclassing from UIViewController the newView. I doubt you could customize the navigation bar on the storyboard.
You have to do it in code.
In viewDidLoad() of newView, you can have these:
var helloButton = UIBarButtonItem(title: "Hello", style: .Plain, target: self, action: "sayHello:")
self.navigationItem.rightBarButtonItem = helloButton
Implement the sayHello method:
func sayHello(sender: UIBarButtonItem) {
}