How to create custom navigation bar programmatically? - ios

I'm developer in
objective c and I need help with with set
custom navigation bar on navigation controller
programmatically I don't use story board or xib.

you can use this, if your targeting iOS 5 and above
[MyNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"customNavBar.png"] forBarMetrics:UIBarMetricsDefault];
or
[[MyNavigationController.navigationBar appearance] setBackgroundImage:navBackground forBarMetrics:UIBarMetricsDefault];
you may need to add custom NavBar Buttons.
also have a look at the Answers here.

If you want more than just customizing appearance, then you can:
Create a subclass of UIView (this will be your custom navigation bar)
Create a custom view controller container that will contain an instance of UINavigationController
Make the instance of UINavigationController not display its navigation bar
Add your custom navigation bar and navigation controller's view as subviews of your custom view controller container (also add your instance of UINavigationController as a child controller of your custom view controller container)
Add methods for popping and pushing view controllers to your custom view controller container
This way you have full control over your custom navigation bar. You can read about view controller containment in the documentation for UIViewController.

Related

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 custom in storyboard my view controller's UINavigationItem that is instantiated by identifier and pushed onto navigation stack?

In storyboard I have UINavigationController with some controllers.
Within the last one I have a button Button. When I tap that button I instantiate another view controller from current storyboard:
But here I would like to custom my UINavigationItem with my custom UIBarButtonItems. How to do this here in storyboard?
Change Top Bar metrics for view controller:

UINavigationItem of ChildviewController

I have Base Controller and Child View Controllers. I want to create UINvaigationItem in Base Controller in storyboard, to make Child controllers inherit it, and show. I create this UINavigationItem in storyboard for Base Controller. Child Controllers not showing this UINavigationItem from Base controller, and it's properties are all nil. How can i make UINavigationItem from BaseController be in ChildViewControllers?
This is picture of storyboard, on the left is Base Controller, on the right is Child ViewControllers.
Basically you can't un-inherit the UINavigation in ChildViewController because it is part of the views stack in the UINavigation, but you can call
// hide navigation bar
self.navigationController.navigationBarHidden = YES;
This will hide the navigation bar on the ChildViewController
You can use the pop methods in the ChildViewController to programmatically control the navigation stack
EDIT
Base on your photo i see you haven't connected all the ViewControlleres in the storyBoard to the navigationViewController.
I recommend not creating the UIViewController like you did.
1) simply add & connect the view controllers you wish to be in the navigation stack
2) select them and in Xcode menu go to Editor->Embed in-> Navigation Controller
That way, all the views will be included in the navigation stack and have a reference to the UINavigationViewController

iOS: pushViewController does not create a custom nav bar in the new view controller

I've created a custom UINavigationBar-derived class (MyNavBar), and would like to use it as the nav bar for my app. I set the MyNavBar class on the nav bar element in the XIB, and also set a custom MyNavItem class on the nav item in the XIB. This works well for the first view controller in my nav controller.
But when I call pushViewController, the newly-pushed controller does not seem to use the custom nav bar item. And for a good reason: I don't know where I can tell the nav controller that it should create a custom nav bar item for every pushed controller... so I guess it creates a UINavigationItem object instead...
Any idea how to ensure that pushViewController will generate my custom nav bar item?

Is there a way to subclass UIToolbar?

I ask this as I would like a global toolbar in my app (similar to the Facebook app with buttons on it). How do I subclass it so that I can add it on all of my ViewControllers? I've created a new class but UIToolbar isn't in the options. How would I modify NSObject manually?
Edit:
My controller hierarchy is as follows:
A login page, which pushes (flip horizontal) -----> Tab Bar Controller. How would I add this toolbar to the top of each page in the tab bar controller?
If you are using a UINavigationController as your window's root view controller, you can let the navigation controller display a toolbar. You need to tell it to show the toolbar by sending it a setToolbarHidden:animated: message. If you want the toolbar to have the same buttons (or other subviews) in all your view controllers, make sure you set each view controller's toolbarItems property to the same array of items.
If you are not using a UINavigationController as your window's root view controller, edit your question and describe your view controller hierarchy.

Resources