Best way to share nav bar across multiple views? - ios

I have three view controllers that I want to have the same navigation bar on but I can't figure out a way to "share" the navbar between those three views. Is there a more efficient way to link up the nav controllers with the view controllers so that I don't have three different navigation controllers even though the navigation controllers are exactly the same?
In addition, I have a tab bar controller if that makes a difference.
Picture to clarify:

As Ch0k0I8 mentioned, UIAppearance is a good way to set a common UI theme throughout the app.
If you are looking for the same functionality between navigation controllers, then you should subclass UINavigationController and implement the common functionality there. Then you can use your custom navigation controller in the storyboard or in code in the 3 places you wish. To put your custom subclass in the storyboard, drag a UINavigationController onto the storyboard like you normally would, then change the class to your custom navigation controller class in the utilities tab. Here is what a custom UINavigationController subclass might look like:
class CustomNavController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// do whatever custom setup stuff you want here
}
// override other methods for different customizations
}

You need to use UIAppearance.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html#//apple_ref/occ/intf/UIAppearance
[[UINavigationBar appearance] setTintColor: color];

You're using a storyboard, and from what you're describing, you don't need three nav controllers. Instead, connect the three UIViewControllers by ctrl-dragging from one to the next to create a push segue. Don't use the UIAppearance proxy; just set customize the bar's appearance in your navigation controller's rootViewController's viewDidLoad method.

Related

Set same navigation bar for UITabBarController child view controllers?

I have a storyboard that looks like this:
Structure:
UINavigationController -> UIViewController
UITabBarController |
UINavigationController -> UIViewController
I would like to apply the same navigation bar for both of the child view controllers of the UITabBarController.
I read on multiple pages in the internet that Apple strictly recommends not to embed a UITabBarController directly within a UINavigationController.
However, I have no idea how to get the same status bar for all of the underlaying controllers (I want all buttons to work the same on all controllers).
As said in the comments, I want both UINavigationControllers to look the same, I want them to navigate through the same controllers and I want to add custom buttons that function the same in every UINavigationController.
Here is a good example of what you wat
basically you can use
UINavigationBar.appearance()
To have the same UI for your navigation bar (or other elements) throughout the aplicaiton.

barButtonItem on tabBarController

i am wanting to add a barButtonItem onto a tabBar so i don't have to use a navigation controller at the top of the screen.
My ViewControllers are embedded in a tabBarController:
And i want to add a barButton on each viewcontroller e.g.:
and them implement some code like:
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 4 {
}
}
to run a function when this button is tapped.
problem is this button isn't showing on the tabBar when the app is built.
i was hoping this would achievable without having to create a custom tabBar.
I don't quite understand what the problem you're describing is. Are you trying to add a 'Logout' button at the top right of your screen by only using a UITabBarController? Why are you against using a UINavigationController? It's incredibly easy to implement using storyboards (which it looks like you're using anyway).
Apple even has a page on how to implement a UINavigationController within a UITabBarController here: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html
Key information here:
To create a combined interface with three tabs that contain custom view controllers and one tab that contains a navigation controller:
Create three custom view controllers, one for each tab, and a navigation controller.
Select the three custom view controllers and the navigation controller (only the navigation controller scene, not it’s root view
controller).
Choose Editor > Embed In > Tab Bar Controller.
Display the tab bar controller as the first view controller by selecting the option Is Initial View Controller in the Attributes
inspector (or present the view controller in your user interface in
another way.)
According to Apple Documentation,
UIBarButtonItem - A bar button item is a button specialized for
placement on a UIToolbar or UINavigationBar object. It inherits basic
button behavior from its abstract superclass, UIBarItem. The
UIBarButtonItem defines additional initialization methods and
properties for use on toolbars and navigation bars.
Since you're against putting a Navigation Controller in your project, a UINavigationBar is not implemented which is what UIBarButtonItems are for. If you want to implement your Menu Button on top of your screen in one of your view controllers. Why not just use a UIButton as inside a UIView like this?? The blue background is just a regular UIView with UIButtons inside of it.

How to use same UINavigationBar for all UIViewControllers

In my app, I have to show same NavigationBar for all UIViewControllers. This NavigationBar has three buttons and these three buttons which will be act as TabBar functionality, that is each tab has its own stack cycle. I have created custom view for NavigationBar with three buttons, but after adding this custom view to HomeViewController, I have to manually add this custom view for all other view controllers. I don't want to do this.
Is there any simple method to achieve this?
There are a couple of ideas that come to mind. First of all, you could use view controller containment and actually have 1 controller that implements your custom nav bar, and then swap out the contained controller as necessary.
If that's not feasible, you can simply use inheritance and have all your custom controllers inherit from a controller that has the nav bar in place.
Another option could be to write your own UINavigationController subclass. I'm not certain if you can override the UNavigationItem behavior, but if you can, you can just just do that -- instead of the UINavigationController taking its child's UINavigationItem to update its own UINavigationBar, the UINavigationBar perhaps just stays the same, like you're expecting/hoping.

Little confusion with tab bar controller and tab bar

I want to provide an opportunity to switch between 3 view controllers.I don't use navigation controller.Instead,I prefer tab bar.Here in xcode we have tab bar controller and tab bar.What are the purposes for providing independent tab bar object?I mean when you drag tab bar Controller it creates controller and 2 items and they already have tab bar.Doesn't it mean I can use it without view controller or what?
Yes, the UITabBar is an independent pre-defined UI element you can use wherever you want. Using it in combination with a UITabBarController gives you the benefits of handling events related to the UITabBar easily, since you don't need to implement most of the logic yourself.
UITabBar is inherit from UIView and while UITabBarController is inherit from UIViewController and responses to the protocols UITabBarDelegate and NSCoding.
Moreover UITabBarController provide automatic mechanism and logic - the other is more flexible in case you have to implement the things differently.

UINavigationBar inside UITableViewController

I have some static cells that I want to display, so I have a UITableViewController. There is also a NavigationBar in this scene that contains some buttons at the top. The setup looks like this:
If I had a UIViewController that contained a UITableView in it, the setup would look like:
So, the question is:
Why does the Navigation Bar have to be embedded inside the UITableView when using a UITableViewController? (I have tried putting it elsewhere but IB won't let me)
I know that UITableView is a subclass of UIView, but is it OK that the top level element in the hierarchy is not a View (but a TableView)?
Thanks.
You shouldn't be placing your UINavigationBar in your UITableView. You should be putting your UITableViewController in a UINavigationController, because that will provide a UINavigationBar for you.
So if you select your UITableViewController in the storyboard, you can choose Embed In -> Navigation Controller from the Editor menu. This would be the proper way to do it.
There are two ways to use a UINavigationBar in iOS:
Embedded inside a UINavigationController (recommended)
As a standalone object
For your particular situation, I'd recommend that you put your UITableViewController as the rootViewController of a UINavigationController. That way you automatically get a navigation bar which you can customize according to your needs. In a typical user experience, when you tap some of your table view rows a new view controller will be pushed onto the navigation stack, so you'll probably end up needing a navigation controller anyway.
What if you decide to use a navigation bar as a standalone object? This is perfectly fine, you can use it inside a view hierarchy as an ordinary UIView, but you'll need to create another object that implements the UINavigationBarDelegate protocol and set it as the delegate property of your navigation bar. If you use a UINavigationController the delegate is already set and configured for you. You also need to add/remove navigation items (instances of UINavigationItem) to your navigation bar by using the pushNavigationItem:animated: and popNavigationItemAnimated: methods.
And about your question on the view hierarchy, you can use a UITableView anywhere a UIView is required. The only caveat is that a UITableView is a view hierarchy on its own and that may restrict your layout a little bit.
The way a UITableViewController works, is its root view is a UITableView. So there is no way to put the UINavigationBar anywhere other than in the UITableView.
I tend never to use a UITableViewController as it doesn't really give you much.
If you particularly want to use the UITableViewController, I don't believe that there is any real problem in having the navigation bar within the table view. You just need to make sure that you set the contentInset on the table view such that the navigation bar doesn't block the content. Though it seems a bit backward to do it this way.
My recommendation would be to just use a normal UIViewController with a navigation bar and a table view.
If you actually need functional navigation, you need to put your UITableViewController within a UINavigationController.
Hope this helps :)
Let me know if anything is still unclear.

Resources