Questions about navigation bar - ios

I am confused about the relationship of navigation controller and content view controllers. In storyboard, navigation bar is under the navigation controller, so I linked it to the NavigationController class and then customized the navigation bar in the viewDidLoad function. However, since different content views will have different navigation bar, like different bar buttons, how can I realize this? Just some basic ideas are good enough. My guess is that I need to specify specific bar buttons in specific content view controllers, but since the navigationBar property is in NavigationController class, how can I refer to it?
I want to hide the title of the navigation bar and make the bar only show some custom buttons (I know toolbar may match it better, but I have other reasons to adopt navigation bar). Please tell me how to hide the title in detail and swift language is preferred.

1) Every ViewController instance has a navigationController property, it' just an optional. To set the bar buttons, you'll want to use navigationItem. So to specify bar buttons you either do so in Interface builder per view controller, or just or do something like this in viewDidLoad
let navBarButton = UIBarButtonItem()...set up bar button
self.navigationItem?.leftBarButtonItem = navBarButton
2) Hiding the title is trivial:
self.navigationItem?.title = ""

Related

do I need to embed navigation controller if I want to make custom navigation bar view?

I need to make a custom navigation bar, since it will have search bar and some other views, it will be easier if I just make custom view instead of inserting view to navigation controller programatically
like the picture below, there are 2 ways to implement custom navigation bar view, by embedding navigation controller (yellow VCs) and use or just using present modally segue (blue VCs)
personally I will choose to use navigation controller because 'maybe' there are some methods that has already built that I can use. but the problem is, the custom navigation view (red color) in navigation controller it seems overlapped by the actual navigation bar in storyboard, I don't know how to hide the navigation bar from navigation controller in storyboard, even though if I use self.navigationController?.setNavigationBarHidden(true, animated: animated), it won't be a problem.
what is the right approach to make custom navigation bar like this?
In IB the navigation bar is shown only to simulate what it might look like when you run the app. The decision to show this is inferred, by default, by the fact that it is downstream from the navigation controller. Luckily you can change this option.

When would you want a UINavigationBar to have multiple UINavigationItems?

I'm curious, in my experience you set the UINavigationItem and then set the title, bar buttons, etc. but UINavigationBar allows you to set an array of these items, why would you want to do this? What benefit would multiple bring?
It's very common when you have a UINavigationController and you push two or more view controllers onto the nav stack. This pushes multiple navigation items on to the nav bar. This is what allows the back button to be titled based on the previous view controller's title.
If you simply add a nav bar to a view then it is unlikely you would have more than one nav item on the nav bar.

iOS Swift: Incorrect Navigation Bar Appearing

The incorrect navigation bar is appearing on my Table View Controller screen (see Storyboard Below).
I would like what's displayed on the storyboard to be my navigation bar (i.e. with "Main Feed" title and Sign Out button on the top right). However, this is what I'm actually getting -
There are two issues here: 1) The incorrect navigation bar is displaying (this one has a login back button); 2) the first few table view cells are placed underneath the nav bar vs. under it.
This happened after I embedded the Table View controller in the Tab Bar Controller. I want a bottom tab bar in the main portion of my application hence the reason why I added the Tab Bar Controller. Any suggestions on how to fix this? Rather than using the Tab Bar Controller in storyboard, is there a way to do this programatically? Thanks!
The navigation bar that you are seeing on top of the stack is the navigation bar from the UITabBarController itself, that is why you are seeing the back button show "login". There's a few ways to work around this, programmatically:
When you initialize the UITabBarController, set it's navigationController's navigation bar property to "hidden"
Go through each view controller form the beginning of your app up to where you first see this problem and in the "init" method of the viewController you are testing, set the navigationbar to hidden. Something like, self.navigationController?.navigationBar.hidden = true;
This is how you can "debug" this issue, but it's going to take some tweaking to get it right.

How to link customized navigation bar to navigation controller?

I created a normal view controller with a customized navigation bar. Later I changed my mind to embed the view controller into a navigation controller. And I noticed that embedding the view controller into a navigation controller will create another navigation bar!
As the image show:
I am wondering if there is a way to replace the navigation bar created by navigation controller with my own customized bar?
Or, if it's not possible, is there a way to configure the new navigation bar? Because for some reason it's not shown in my interface builder.
You have 2 solutions
Hide the system navigationbar using below code so it was display your custom navigation.
self.navController.navigationBarHidden = YES;
Customize the system navigation bar with its background color, back button and title text color. For this you can refer a below links which provide you detail of its
http://www.appcoda.com/customize-navigation-status-bar-ios-7/
You can change the color and buttons of the UINavigationBar provided by the system and make it look like yours.

Hide Navigation Bar in Interface Builder w/ Storyboards

I have a pretty basic storyboard based app, with a UINavigationController, a main view and a secondary view, both of which are in the navigation hierarchy. I’m currently hiding the navigation bar on the main view by using setNavigationBarHidden: as appropriate in viewWillAppear and viewWillDisappear. It seems like there should be a way to do this in Interface Builder, rather than in code. Essentially I’d like the options available in the Simulated Metrics options, but not simulated. Does that exist?
In the scene for the UINavigationController itself, I suppose you could check the hidden button for the nav bar in the Attributes inspector, but that would make the nav bar hidden for all the view controllers in the navigation stack. But that's not what you want.
To hide the nav bar for an individual view controller using IB, you would need to use a stand alone nav bar for each view controller (e.g., drap and drop a Navigation Bar from the object library in IB). To use a stand alone nav bar in a navigation stack would be more work than programmatically hiding the nav bar.
Just uncheck the Attribute Inspector > Shows Navigation Bar in Attribute Inspector

Resources