Navigation Bar isn't not showing up - ios

I'm using MGSplitViewController for my iPad application.
I have added a viewController to it's detailViewController this way:
my2ndVC *vc = [[my2ndVC alloc] init];
splitViewController.detailViewController = vc;
It works perfect, now when I'm adding a navigationBar to my2ndVC
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 448, 44)];
[self.view addSubview:navBar];
The bar isn't showing up.
I know I can add my2ndVC like this:
my2ndVC *vc = [[my2ndVC alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
splitViewController.detailViewController = navController;
But then I also have a condition in one of my view controller:
if ([splitViewController.detailViewController isKindOfClass:[my2ndVC class]])
What would be the suggested workaround?

Cant you change your condition as follows:
if ([splitViewController.detailViewController.topViewController isKindOfClass:[my2ndVC class]])
To Make it simpler :
UINavigationController *aNavigationController = (UINavigationController *)splitViewController.detailViewController;
if ([aNavigationController.topViewController isKindOfClass:[my2ndVC class]]) {
// TRUE...
}

Related

iOS 7: different navigation items for tab bar controller

I am relative new with the iOS app development. Currently I am developing a small app with a tab bar. The problem I am facing is that I would like to have different navigation items foreach tab. I tried a lot of things, but things aren't working. I am programming in the native iOS language.
In my app I've got a AppDelegate. In my AppDelegate there is a little piece of code for setting up my mainViewController:
- (void)setupOverlordViewController
{
MainViewController *rootVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navVC;
}
I am setting up my tabs in my MainViewController:
- (void)viewDidLoad
{
UIViewController *tabView1 = [[Tab1ViewController alloc] init];
UIViewController *tabView2 = [[Tab2ViewController alloc] init];
UIViewController *tabView3 = [[Tab3ViewController alloc] init];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:tabView1];
[tabViewControllers addObject:tabView2];
[tabViewControllers addObject:tabView3];
[self setViewControllers:tabViewControllers];
tabView1.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView1", nil)
image:[UIImage imageNamed:#"tabView1.png"]
tag:1];
tabView2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView2", nil)
image:[UIImage imageNamed:#"tabView2.png"]
tag:2];
tabView3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView3", nil)
image:[UIImage imageNamed:#"tabView3.png"]
tag:3];
}
Each View (tabView1, tabView2, tabView3) has there own layout, which is set in the ViewDidLoad method of the View. When I would like to add navigation buttons in the navigation bar by adding them in the ViewDidLoad method, but it seems impossible to add the buttons. The only way to add them is directly in my MainViewController, but then I can't set the navigation bar buttons different foreach tab.
The code for adding the buttons to my navigation bar is as follows:
UIBarButtonItem *btnNewRecord = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(btnNewRecord)];
NSArray *rightItems = [NSArray arrayWithObjects:btnNewRecord, nil];
[self.navigationItem setRightBarButtonItems:rightItems];
Could somebody explain me what I am doing wrong?
I have created a example for you by using xib files. I have created three View Controllers and added them to navigation controllers. Following the appdelegate code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *firstNavVC = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController: secondVC];
ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:#"ThirdView" bundle:nil];
UINavigationController *thirdNavVC = [[UINavigationController alloc] initWithRootViewController: thirdVC];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:firstNavVC];
[tabViewControllers addObject:secondNavVC];
[tabViewControllers addObject:thirdNavVC];
firstNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"First", nil)
image:nil
tag:1];
secondNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Second", nil)
image:nil
tag:2];
thirdNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Third", nil)
image:nil
tag:3];
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.viewControllers = tabViewControllers;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
return YES;
}
Following is the output :
You can download code example here
you need separate navigation controller for each tab bar view controller & then you can add UIBarButtonItem on each navigation controller.

Tab Bar not hiding Using DDMenuController (Fb like sliding menu)

I am facing this issue from last two days, but could find any solution on it. Can some one help. This is the code snippet am using for TabBar viewControllers.
// Set Up Tab Bar
NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:5];
self.tabBarController = [[UITabBarController alloc] init];
[tabBarController
setDelegate:self];
UINavigationController *navController = nil;
NSArray *vcArray = [self papulateViewControllers];
// SetViewController for tab Bar
-(NSArray *) papulateViewControllers{
BrowseViewController *browseVC = [[BrowseViewController alloc] initWithNibName:#"BrowseViewController" bundle:nil];
AlbumViewController *albumVC = [[AlbumViewController alloc] initWithNibName:#"AlbumViewController" bundle:nil];
SubmitStep1VC *submitVC = [[SubmitStep1VC alloc] initWithNibName:#"SubmitStep1VC" bundle:nil];
// SubmitStep1VC *submitVC = [[SubmitStep1VC alloc] initWithNibName:#"SubmitStep1_iPhone5.xib" bundle:[NSBundle mainBundle]];
WallViewController *wallVC = [[WallViewController alloc] initWithNibName:#"WallViewController" bundle:nil];
OptionVC *optionVC = [[OptionVC alloc] initWithNibName:#"OptionVC" bundle:nil];
sliderVCRef = [[SliderVC alloc] initWithNibName:#"SliderVC" bundle:nil];
//Navigation Controllers
UINavigationController *browseNavController = [[UINavigationController alloc] initWithRootViewController: browseVC];
[browseNavController setNavigationBarHidden:YES];
UINavigationController *albumNavController = [[UINavigationController alloc] initWithRootViewController: albumVC];
[albumNavController setNavigationBarHidden:YES];
UINavigationController *submitNavController = [[UINavigationController alloc] initWithRootViewController: submitVC];
[submitNavController setNavigationBarHidden:YES];
UINavigationController *wallNavController = [[UINavigationController alloc] initWithRootViewController: wallVC];
[wallNavController setNavigationBarHidden:YES];
UINavigationController *optionNavController = [[UINavigationController alloc] initWithRootViewController: optionVC];
[optionNavController setNavigationBarHidden:YES];
DDMenuController *browseMenuController = [[DDMenuController alloc] initWithRootViewController:browseNavController];
self.menuController = browseMenuController;
self.menuController.leftViewController = sliderVCRef;
DDMenuController *albumMenuController = [[DDMenuController alloc] initWithRootViewController:albumNavController];
albumMenuController.leftViewController = sliderVCRef;
DDMenuController *submitMenuController = [[DDMenuController alloc] initWithRootViewController:submitNavController];
submitMenuController.leftViewController = sliderVCRef;
DDMenuController *wallMenuController = [[DDMenuController alloc] initWithRootViewController:wallNavController];
wallMenuController.leftViewController = sliderVCRef;
DDMenuController *optionMenuController = [[DDMenuController alloc] initWithRootViewController:optionNavController];
optionMenuController.leftViewController = sliderVCRef;
/// Works fine if i uncomment this line and comment next line of code (Passing Viewcontrollers is fine )
// return [NSArray arrayWithObjects:self.menuController, albumVC, submitVC, wallVC, optionVC, nil];
////******* issue in case i use this line (Passing menuController creates issue of Tabbar )
return [NSArray arrayWithObjects:self.menuController, albumMenuController, submitMenuController, wallMenuController, optionMenuController, nil];
////////////
}
When i try to push to push to any viewcontroler from any above TabBarController Tab bar is not hiding . example
grandPrizeVC.hidesBottomBarWhenPushed = YES;
Its keep showing me tab bar.
If i try
appDelegate.tabbarcontroller.tabbar.hidden = YES; It shows on a black bottom bar on new VC.
Your app doing just what you have implemented. You are adding your sliding menu view controller as subview controller to tabbar controller, of course, it won't hide. Some suggestions to hide tabbar:
1. Add tabbar controller as modal to your DDMenuController
2. write some methods to hide/show tabbar (searching hide tabbar will give you the answers, or you could just traverse subviews of tabbar controller's view find tabbar and hide it).
Good luck!
Following worked for me :
Show TabBar :
+ (void) showTabBar{
MyAppDelegate* appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication]delegate];
UITabBar *tabBar = appDelegate.tabBarController.tabBar;
UIView *parent = tabBar.superview; // UILayoutContainerView
UIView *content = [parent.subviews objectAtIndex:0]; // UITransitionView
UIView *window = parent.superview;
CGRect tabFrame = tabBar.frame;
tabFrame.origin.y = CGRectGetMaxY(window.bounds) - CGRectGetHeight(tabBar.frame);
tabBar.frame = tabFrame;
CGRect contentFrame = content.frame;
contentFrame.size.height -= tabFrame.size.height;
}
Hide TabBar:
+(void) hideTabBar{
MyAppDelegate* appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication]delegate];
UITabBar *tabBar = appDelegate.tabBarController.tabBar;
UIView *parent = tabBar.superview; // UILayoutContainerView
UIView *content = [parent.subviews objectAtIndex:0]; // UITransitionView
UIView *window = parent.superview;
[UIView animateWithDuration:0.01
animations:^{
CGRect tabFrame = tabBar.frame;
tabFrame.origin.y = CGRectGetMaxY(window.bounds);
tabFrame.origin.y +=20;
tabBar.frame = tabFrame;
content.frame = window.bounds;
}];
}

The correct way of adding a UINavigationController to a existing UIViewController

I'm trying to add a UINavigationController to my existing view controller by adding this in ViewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
navController = [[UINavigationController alloc]init];
[self.view addSubview:navController.view];
}
But by doing it this way, it completely blocks my view. It puts a UINavigationBar on the top but the rest of the view doesn't respond to input.
This is how I am presenting the view. The SecondViewController is the one I want to have a NavController:
UITabBarController *tabController = [[UITabBarController alloc] init];
FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"CardsViewController" bundle:nil];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"First"
image:[UIImage imageNamed:#"img1.png"] tag:1];
[viewController1 setTabBarItem:tab1];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"ShoppingViewController" bundle:nil];
UINavigationController *SecondViewNavCont = [[UINavigationController alloc]initWithRootViewController:viewController2];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:#"Second"
image:[UIImage imageNamed:#"img2.png"] tag:2];
[SecondViewNavCont setTabBarItem:tab2];
UIViewController *viewController3 = [[UIViewController alloc] init];
UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:#"Third"
image:[UIImage imageNamed:#"img3.png"] tag:3];
[viewController3 setTabBarItem:tab3];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
nil];
[self.view addSubview:tabController.view];
You cannot add it to the current view controller
What you need to do is instead of presenting the ViewController is to add this viewcontroller to the navigationview controller and present that
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:YourPresentedViewController];
//then present the navController
[self presentModalViewController:navController animated:YES];
Now when you present it do the following
NSArray arrayWithObjects:viewController1,
SecondViewNavCont,
viewController3,
nil];
You can try this code for load uiviewcontroller to uinavigationcontroller:
yourviewController *viewcontroller=[[yourviewController alloc] initWithNibName:#"yourviewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller];
[self presentModalViewController:navController animated:YES];
(or) you want to load uinavigationcontroller in application startup try below code in your application delegate class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller.view];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Welcome!
You need to set the RootViewController for the navController object. Then further you can push UIViewcontroller objects using pushViewController method.
You need to add a view for that Navigation Controller. The added Navigation Controller has no view so it was overlaying on the UIViewController
You need to add
[[UINavigationController alloc] initWithNibName:#"ViewControllerName" bundle:nil];

How to change the color of UITabbarViewController's navigationBar?

There is a UITabBarController
- (void)getToMCGatherViewController
{
mCGatherViewController = [[MCGatherViewController alloc] initWithNibName:#"MCGatherViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mCGatherViewController];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
}
In the .h file:
#interface MCGatherViewController : UITabBarController
In the .m file . I want to change the color of the view's navigationBar
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.tintColor=[UIColor greenColor];
}
and it does not work at all.
Help me with this problem , thank you in advance!
just add
[navigationController.navigationBar setTintColor:[UIColor greenColor]];
after
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mCGatherViewController];
in your getToMCGatherViewController method.
Just edit your code like this and try I think this will work for you.
mCGatherViewController = [[MCGatherViewController alloc] initWithNibName:#"MCGatherViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mCGatherViewController];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.view addSubview:nav.view];
Then you change your Navigation Bar tint color.

Toolbar + Navigation Bar + Segmented Control?

I'm trying to find a way to layout an application that includes a tab bar on the bottom, a navigation bar on the top, and a row of buttons on the navigation bar that switches views (on the first tab).
I've drawn a very rough sketch (sorry!), but I hope it illustrates the intent.
On the bottom, there are two tabs (tab1, and tab2).
When Tab1 is selected, the navigation bar will have 3 buttons that will show different views (tab1_1, tab1_2, tab1_3).
When Tab2 is selected, the navigation bar won't show any buttons, but rather some simple text.
At this point, I have the following scheme in my application delegate's didFinishLaunchingWithOptions:
UIViewController *viewController1 = [[Tab1_ViewController alloc] initWithNibName:#"Tab1_ViewController" bundle:nil];
UIViewController *viewController2 = [[Tab2_ViewController alloc] initWithNibName:#"Tab2_ViewController" bundle:nil];
tab1NavController = [[UINavigationController alloc] initWithRootViewController:viewController1];
tab2NavController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:tab1NavController, tab2NavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
I was wondering if I need to redo how I'm doing things in order to achieve the layout as in the picture.
Any help would be appreciated, thank you!
I have done this for my current project...i hope this will help you....
At first take UITabbarController at your first viewController [first sketch you have given]
For your first view use this code....
- (void)viewDidLoad {
[super viewDidLoad];
dashBoardView = [[DashboardViewController alloc] initWithNibName:#"DashboardViewController" bundle:nil];
dashBoardView.title = #"dashBoardView";
UINavigationController *mydashboarController = [[[UINavigationController alloc] initWithRootViewController:dashBoardView] autorelease];
mydashboarController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:mydashboarController];
[dashBoardView release];
ordersView = [[OrdersViewController alloc] initWithNibName:#"OrdersViewController" bundle:nil];
ordersView.title = #"ordersView";
UINavigationController *myorderController = [[[UINavigationController alloc] initWithRootViewController:ordersView] autorelease];
myorderController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderController];
[ordersView release];
orderList = [[OrderListViewController alloc] initWithNibName:#"OrderListViewController" bundle:nil];
orderList.title = #"orderList";
UINavigationController *myorderListController = [[[UINavigationController alloc] initWithRootViewController:orderList] autorelease];
myorderListController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderListController];
[orderList release];
productView = [[ProductViewController alloc] initWithNibName:#"ProductViewController" bundle:nil];
productView.title = #"productView";
UINavigationController *myproductController = [[[UINavigationController alloc] initWithRootViewController:productView] autorelease];
[listOfViewControllers addObject:myproductController];
[productView release];
[self.tabBarController setViewControllers:listOfViewControllers animated:YES];
NSArray *segmentTextContent = [NSArray arrayWithObjects:NSLocalizedString(#"Dashboard", #""),NSLocalizedString(#"Order", #""),
NSLocalizedString(#"Product", #""),NSLocalizedString(#"More", #""),
nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 400, 40);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
segmentedControl.tintColor = [UIColor colorWithHue:8.0 saturation:8.0 brightness:8.0 alpha:1.0];
segmentedControl.alpha = 0.8;
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
If it is not clear to you then please knock...

Resources