iOS - Access Navigation Controller in AppDelegate - iphone

I have built my app using Storyboards. I have a UITabBarController and a NavigationController embedded it that.
I am trying to customise my app with tint colors and I have managed to set a custom color for the Tabbar. I was able to access the tabbar from the rootViewController, however I cannot get access to the navigation bar. Is there anyway that I can easily access this?
Sample of my AppDelegate code is below.
Thanks
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.tabBarController = (UITabBarController*)self.window.rootViewController;
UITabBar *tabBar = self.tabBarController.tabBar;
[tabBar setSelectedImageTintColor:[UIColor greenColor]];
tabBar.tintColor = [UIColor colorWithRed:0.4 green:0.522 blue:0.129 alpha:1];
//This bit doesnt work
UINavigationController *navCon = self.tabBarController.navigationController;
navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161 alpha:1];
return YES;
}

If you're using navigation controllers, would expect them to embed the view controllers that are managed by the tab bar controller.
Try NSLog(#"%#", [self.tabBarController viewControllers]); and see whether it lists navigation controllers or your custom controller. For example, I created two view controllers that are triggered by my tab bar, one with and one without navigation and the above line shows:
(
"<UINavigationController: 0x6a35f20>",
"<SecondViewController: 0x6a39500>" )
So, I could get mine as [[self.tabBarController viewControllers] objectAtIndex:0].

Assuming you did no work with IBOutlet's, I suggest you maybe use them along with the XIB file. For example, In the header file:
IBOutlet UINavigationBar *aNavBar;
And then in the .m, switch:
UINavigationController *navCon = self.tabBarController.navigationController;
navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161 alpha:1];
With:
[aNavBar setTintColor:[UIColor colorWithRed:0.384 green:0.259 blue:0.161 alpha:1]];
And there you go! I made this quick, so tell me if it works or not.
-Justin A.

Related

Separator between navigation bar and view - iOS 7

In iOS 7 there's a grey separator between the navigation bar and the view.
Back in iOS 6, there wasn't that horizontal line, thus the view would blend in with the navigation bar as if they were the same image. Now I don't know how to remove it...
I've tried resizing the view / navigation bar, but it doesn't help. Any ideas?
The other answers did not work for me. To remove the separator, I had to set the background image AND the shadow image, like so:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
Add this:
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
in your AppDelegate.m in the application didFinishLaunchingWithOptions method
Try with
self.navigationController.navigationBar.translucent = NO;
In your viewDidLoad method and let me know :)
If you need this effect on every ViewController, you could simply do:
[[UINavigationBar appearance] setTranslucent:NO]
Or you'll need to do this where you first instantiate the navigation controller. For example, if the navigation controller is the root view controller of your app you can simply do
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
nav.navigationBar.translucent = NO;
in your
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
If, on the other end, you instantiate it through a segue you could do (in the appropriate view controller)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"navController"]){
UINavigationController *nav = (UINavigationController *)segue.destinationViewController;
nav.navigationBar.translucent = NO;
}
}
And so on (if you're actually instantiating it from code, it should be the easiest option).

Tab bar controller controller color change

In my application i want to change the tab bar controller color,How to assign a custom color to the tab bar controller lik uinavigation bar in ios6?Can any one give me some refrences?
Put this on app delegate:
UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
tabBarController.view.tintColor = [UIColor redColor];
It's better than the first answer because it also changes the edit view tint color.
You can use the setTintColor option as described
[tabbarController.tabBar setTintColor:[UIColor greenColor]];
or you can set the background image
[tabbarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_bg.png"];
if your TabBarController is defined int the AppDelegate you may need additional coding to access it.
First to set the background image
[[[[(UITabBarController *)[[(AppDelegate *)[UIApplication sharedApplication].delegate window] rootViewController]tabBar]setBackgroundImage:[UIImage imageNamed:#"tab_bg.png"]]]];
Second to set the tintcolor if required
[[[[(UITabBarController *)[[(AppDelegate *)[UIApplication sharedApplication].delegate window] rootViewController]tabBar]setTintColor:[UIColor redColor]]]];
dont forget to import your AppDelegate.h file.
You can use this call
tabbarController.tabBar.tintColor = [UIColor redColor];
To change only tabBar color, you can achieve by this:
tabbarController.tabBar.tintColor = [UIColor redColor];
But, for more, you need to create Custom TabBar and can do with it like color change, custom tabbar icon changes, etc.
Hope, it will be helpful to you.
Cheers.
Approach that worked for me (tested in iOS6) is:
[[UITabBar appearance]setTintColor:[UIColor redColor]];
in AppDelegate.h file in method application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. Try this if it's still actual. I see the question still stays unsolved.

UIPopoverController and UINavigationController cuts corners

I have a problem with the display of my popover. After initWithContentViewController: and presentPopoverFromBarButtonItem:permittedArrowDirections:animated: it cuts corners of the navigation bar. How should I fix it?? Thanks.
This is the code I'm using
NavContr *nav = [NavContr new];
nav.navigationBar.backgroundColor = [UIColor redColor];
UIPopoverController *tempPop = [[UIPopoverController alloc] initWithContentViewController:nav];
[tempPop presentPopoverFromBarButtonItem:mainButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
EDIT: I have resolved this problem:
+ (void)configure:(UINavigationController *)navController {
UINavigationBar *navigationBar = navController.navigationBar;
UIView *contentView = nil;
for (UIView *view in navController.view.subviews) {
if ([[NSString stringWithFormat:#"%#", [view class]] isEqualToString:#"UILayoutContainerView"])
contentView = view;
}
// setting frame to navigation bar and content view
[navigationBar setFrame:CGRectMake(navigationBar.frame.origin.x, 0, navigationBar.frame.size.width, navigationBar.frame.size.height)];
[contentView setFrame:CGRectMake(contentView.frame.origin.x, 0, contentView.frame.size.width, contentView.frame.size.height + navigationBar.frame.size.height)];
[navController.view bringSubviewToFront:contentView];
for (UIView *customView in contentView.subviews)
customView.frame = CGRectMake(customView.frame.origin.x, customView.frame.origin.y + navigationBar.frame.size.height, customView.frame.size.width, customView.frame.size.height);
[contentView addSubview:navigationBar];
[contentView bringSubviewToFront:navigationBar];
}
This is probably because you have no root view controller, or are otherwise fiddling with the navigation controller in ways it was not meant to be played with. This is how you ought to be setting up the popover:
MyCustomViewController *viewController = [[UIViewController alloc] initWithNibName:#"MyCustomViewController" bundle:nil]; //or storyboard or whatever
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController]; //you should have a root view controller before displaying the popover
tintColor = [UIColor redColor];
UIPopoverController *tempPop = [[UIPopoverController alloc] initWithContentViewController:nav];
[tempPop presentPopoverFromBarButtonItem:mainButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
There are a few very important things going on here:
Your navigation controller should have a root view controller before you display it.
This code is using a standard UINavigationController instance. According to the documentation, you should not subclass UINavigationController, nor should you try and reinvent the wheel. Apple has created a complex and comprehensive framework, UIKit, that you can use to build amazing apps. If you try and step outside the box, you'll be creating an awful lot of work for yourself without any appreciable benefit.
This is using the tintColor property of the UINavigationBar class. If the tint is insufficient for your UI, you can also set the background image manually (refer to the docs).
If you want to make a popover with a navigation controller, use the built-in UINavigationController class. Don't subclass it and don't reinvent it. To customize the appearance of the navigationBar, use the UI_APPEARANCE_SELECTOR methods in the UINavigationBar class.
I get the solution before add CALayer the UIPopOverController shows like
after adding below lines in table view class i get the following UIPopOverController
#import <QuartzCore/QuartzCore.h>
CALayer *imageLayer2 = self.tableView.layer;
[imageLayer2 setCornerRadius:-20];
[imageLayer2 setBorderWidth:1];
Try it in your project may be it works!!
Thanx
I have tried & replicate the issue you are facing, made some R&D. It's due to the line of code below :
nav.navigationBar.backgroundColor = [UIColor redColor];
While you set the background color of the navigation bar it will behave weird due the native shape of the pop up. Try and remove the below line, you will definitely have issue resolved.
If you are specifying the Rect where the popover appears, we've found that using decimals can result in weird distortions like that. Be sure you're using whole number for origin and size.

Changing the navigationBar on pushViewController:

In my tabBar based app I have subclassed the UINavigationBar. Let's say I have three of them: BlueNavBar, BlackNavBar and RedNavBar. It looks something like this:
//BlueNavBar.m
- (void)drawRect:(CGRect)rect {
self.tintColor = [UIColor colorWithRed:65.0f/255.0f green:(156.0f/255.0f) blue:(215.0f/255.0f) alpha:1.0];
UIImage *image = [[UIImage imageNamed:#"blueNavBar.png"]retain];
[image drawInRect:rect];
[image release];
}
I've assigned the subclassed navigationbar for each tab with Interface Builder. That is working great, no problems there.
In some viewControllers however i want to change the navigationBar during "pushViewController". Let's say I want to change the current navigationbar (which is for e.g. BlueNavBar) to the RedNavBar. How can I do this programmatically, without Interface Builder?
It depends on how you've designed the view controller classes themselves. One way to design what you need would be to set the navigation bar type (i.e. colour) when you create the view controller, before you push it on the stack. Something like:
SomeViewController* someViewController = [[SomeViewController alloc] initWithNibName:#"SomeView" bundle:nil];
someViewController.navigationBarStyle = NBStyleRed; // NBStyleRed defined as an enum somewhere
[self.navigationController pushViewController:someViewController animated:YES];
[someViewController release];
The setter method for navigationBarStyle would then (re)create an appropriately-coloured navigation bar for the view controller.

How to set iPhone tab bar icon in code?

I am using the same ViewController for several different views.
When instantiating the ViewController for a specific view, is there an easy way to specify the tab bar icon via code?
yourViewController.tabBarItem = [[UITabBarItem alloc]
initWithTitle:NSLocalizedString(#"Name", #"Name")
image:[UIImage imageNamed:#"tab_ yourViewController.png"]
tag:3];
The viewControllers are added to the tab bar, so the image and names should be set before the tab bar becomes visible (appDelegate if they are there on app start for instance). After that, you could use the above code to change the icon and text from the loadView or viewDidAppear within that viewController.
Yes. Your UITabBar has a property called items, which is an array of UITabBarItems for each tab bar item. You can create a UITabBarItem using the –initWithTitle:image:tag: constructor to use your own image, or the –initWithTabBarSystemItem:tag: constructor to use a system image.
You can also do this in the AppDelegate, by declaring a UITabBarController iVar and pointing it to the applications tabBarController. You can access the individual titles using the items array. and the setTitle.
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.tabBarController = (UITabBarController*)self.window.rootViewController;
NSArray* items = [self.tabBarController.tabBar items];
[[items objectAtIndex:0] setTitle:#"Home"];
[[items objectAtIndex:1] setTitle:#"Cool"];
[[items objectAtIndex:2] setTitle:#"Stuff"];
[[items objectAtIndex:3] setTitle:#"Settings"];
return YES;
}
Correct way is : Add this below line in viewDidLoad
[self.tabBarItem setImage:[UIImage imageNamed:#"<Image Name>"]];
to viewcontrollers which are set inside UITabBarController