UINavigationController right bar button - ios

I have this code in AppDelegate
LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navLogin = [[UINavigationController alloc] initWithRootViewController:loginVC];
navLogin.navigationBar.topItem.rightBarButtonItem = myRightBarButton;
self.window.rootViewController = navLogin;
I want to set the right bar button for this navigation controller where it won't change the button when pushing new viewcontrollers.
How can achive this ?

you had to set right bar button in each viewcontroller manually . you can use containerview then embed in in your Mainviewcontroller .
then from containerview -> embed segue to new navigationcontroller
adding view alignment should be like this
view {
->button
->containerview
}
like this,
then each pushed viewcontroller will have the right button automatically
you can set height width of viewcontroller 320 480 i.e all childviewcontroller get fullscreen

Related

Navigation bar not accessible when using SWRevealViewController with UITabBarController

I have set Sliding menu(Rear view controller) using SWRevealViewController with a UITabBarController (front view controller). Below is the piece of code I used:
RearMenuViewController *rearViewController = [[RearMenuViewController alloc] init];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
UINavigationController *navTabController = [[UINavigationController alloc] initWithRootViewController:_tabBarController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:navTabController];
revealController.delegate = self;
revealController.rightViewController = nil;
self.viewController = revealController;
self.window.rootViewController = revealController;
This works fine and I can toggle between the rear menu and tab bar items using swipe gesture.
But I am not able to set the navigation title for each tab bar item nor I can add any bar button on navigation bar. I can only see blank navigation bar for every tab bar item.
Also would like to share that I am NOT using storyboard in the project.So, I need to set the title and a bar button through code only.
Please let me know if any other info is required.
I managed to access the navigation bar for each tab bar item like this:
UINavigationController *currentNav = (UINavigationController *)self.tabBarController.parentViewController;
UINavigationItem *navItem =(UINavigationItem *) [currentNav.navigationBar.items firstObject];
navItem.title = #"Dashboard";
Need to write this in each item (view controller) of tab bar if we need to have different titles for each tab bar item.
Please let me know if there is a better way to achieve this.

tab bar not displayed when added to navigation controller IOS

I have a navigation controller which has view controller(VC1) . This view controller has 3 button out of which 1 directs to a tab bar controller. From tab1 of the tab bar controller a button is there which navigates to a View controller(vc2).
The problem is VC2 is not displaying the tab bar. How can I make my VC2 display the tab bar.
Navigation Controller—> View Controller-->Tab bar Controller —>Tab1 -> View Controller (does not show tab bar)
I am doing this in IOS
Using a tabbarcontroller within a navigation controller is not recommended by Apple. However it is possible to do so.
In the VC1, write the following code.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
MyStartViewController *startController = [[MyStartViewController alloc] initWithNibName:#"MyStartViewController" bundle:Nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:startController];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.viewControllers = #[navController, viewController2];
[self.navigationController pushViewController:tabBarController animated:YES];
Now within startController, add a UIButton. And in the button action, push the new VC2 from it.
Button action:
- (IBAction)buttonPressed {
MyViewController2 *vc2 = [[MyViewController2 alloc] initWithNibName:#"MyViewController2" bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
}
Hope this will serve your purpose.

How to add a Back button to a NavigationBar?

How do I add the back button to a navigation bar? I know there are some related questions but none of them helped. I'm not using storyboard nor did I start with the Master Detail template. Is there a setting to do this? or do you add it by code? if so what code do I need? Any help is appreciated! THANKS!
The back button will appear by default if you use a navigationController correctly. The ViewControllers should be this
NavigationController > FirstViewController > SecondViewController
You will need to create navigationController and instantiate with firstVC as the root. From firstVC you can push secondVC and the back button will be there.
The following placed into appDelegate application didFinishLaunchingWithOptions: will load firstVC initially. Place this after you initialize the window...
UIViewController *firstVC = [[UIViewController alloc] initWithNibName:#"firstVC" bundle:nil];
// any setupup of firstVC
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:firstVC];
[self.window setRootViewController:navCon];
Then in your firstVC class if you want to push secondVC
in firstVC.m:
UIViewController *secondVC = [[UIViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
You don't need to explicitly add a back button. The button will be added automatically when you push controllers into a UINavigationController. That is, a call as:
[navigator pushViewController: controller animated: ...];
creates a back button.

load page with pushViewController on tabbar

I have UIViewController with tabbar botton, and navigation bar and navigation item,
when I press on my button in navigation Item I want to load a view, I don't know how to load this view on tabbar,
would you please help me,thanks in advance!
-(IBAction) infoPage:(id)sender
{
InfoCtrol *i = [[InfoCtrol alloc] initWithNibName:#"InfoCtrol" bundle:nil];
[self.navigationController pushViewController:i animated:YES];
}
This will load the InfoCtrol controller and set the Touch UpInside Event in xib to the infoPage method
Do you mean you want to hide the tab bar when you push new controller in navigation?
If true. There is a property hidesBottomBarWhenPushed in the UIViewController class.
// Instead of adding ViewController to TabbarController, add NavigationControllers.
// Eg.
UINavigationController *NavController = [[UINavigationController alloc] initWithRootViewController:_viewCtrl1];
[watchListNavController.navigationBar setTintColor:[UIColor blackColor]];
_tabC = [[UITabBarController alloc] init];
_tabCt.viewControllers = [NSArray arrayWithObjects:NavController,_viewCtrl2,_viewCtrl3, nil];
// Now you can use Push and Pop In your _viewCtrl1.
// Do same with all the viewController

presentModalViewController not displaying toolbar?

I have a viewcontroller I am trying to display as a UIModalPresentationFormSheet, however it does not show its UIToolbar. How can I make a toolbar show up on it?
ConnectionEditViewController * connectionEditViewController = [[ConnectionEditViewController alloc] initWithNibName:#"ConnectionEditViewController" bundle: nil];
connectionEditViewController.modalInPopover = YES;
connectionEditViewController.modalPresentationStyle = UIModalPresentationFormSheet;
connectionEditViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: connectionEditViewController animated: YES];
Create a UINavigationController instance, set the rootViewController to be your connectionEditViewController, then configure the modal properties for the UINavigationController and present that.
If this is on iPad, it should pop up the view with the Navigation Bar as a grey toolbar at the top.
Configure the navigationItem of the connectionEditViewController to configure the nav bar.

Resources