Tab bar controller controller color change - iphone

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.

Related

NavigationBar setShadowImage not always working

I'm trying to set a custom shadow image for the navigation bar in my table views, but it's only showing in some views. I've created a super class to set the styles for my table views.
- (void)viewDidLoad
{
[super viewDidLoad];
// Set navigation bar background
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationbarbackground.png"] forBarMetrics:UIBarMetricsDefault];
// Set navigation bar shadow imag
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:#"navigationbarshadow.png"]];
In the view I see at starting my app, no shadow is showed. But when I touch the [+] button in my navigation bar to open my 'add new item' table view, it does show a shadow.
Could someone point me in the right direction here?
you need to set custom backgroudImage for UINavigationBar, then the shadowImage can work.
The Appearance proxy should work.
Just call it somewhere (e.g. in your AppDelegate) upon startup.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeAppearance];
return YES;
}
- (void) customizeAppearance
{
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationbarbackground"] forBarMetrics:UIBarMetricsDefault];
// Set the shadow image for *all* UINavigationBars
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"navigationbarshadow.png"]];
//add other appearance stuff here...
}
However if you create a storyboard with multiple UINavigationController's in it and a bunch of segue's pushing navigation controller's you might get a corrupt view controller structure which might be the problem here.
Another possible issue might be the Clip Subviews option of a Navigation Bar somewhere in your nib file or you storyboard. Make sure it is turned off if you want the shadow (image)!
By the way, if you use imageNamed you don't need to include the file extension.
Try this !
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"navbar-iphone.png"]];

iOS - Access Navigation Controller in AppDelegate

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.

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.

Hide title of navigation bar without removing it

I have a customized navigation bar with a image background. I do want to show the title on the background but I need its text for the back button in the next view.
self.title=#"" will not put (naturally) in the back button the previous title.
Based on the suggestion of pheekicks, I found a tip to do it:
UILabel *label = [[UILabel alloc] init];
self.navigationItem.titleView = label;
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil]];
if you want to switch between view controllers, and you want to hide title text of navigation bar, that still appear back button, in root view controller, you should override this method:
- (void) viewDidAppear:(BOOL)animated{
self.navigationItem.titleView = m_anyViewYouWant;
}
This is OK!
This is a fairly old post. But I got around this problem by setting the title in the viewWillDisappear method, so it does not show when the view is displayed, but shows in subsequent views back button.
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self setTitle:NSLocalizedString(#"mytext", nil)];
}
Try:
self.titleView.hidden = YES;
I'm using this line to hide the navigation bar on viewDidLoad:
self.navigationController.navigationBarHidden=YES;

change color of navigation bar

How can I change the color of navigation bar from its default blue color?
Thanks.
The UINavigationBar class has a UIColor *tintColor property that you can change in code.
Alternately this property is also exposed in the InterfaceBuilder UI design tool.
Assuming you have added the navigation bar programmatically and not in Interface Builder, just put this in your viewDidLoad method.
self.navigationController.navigationBar.tintColor = [UIColor grayColor];
TintColor property doesn't affect default subview of navigation bar as bottom border and shadow. Sometimes it's useful to override layout of navigation bar at all.
Despite navigationBar is read-only property for UINavigationController you can avoid
this restriction by "setValue:forKey:". This method was approved on 5 applications successfully submitted to AppStore.
You can subclass UINavigationBar and change drawRect: method as you want.
For example,
#implementation CustomNavigationBar
- (void) drawRect:(CGRect)rect
{
[super drawRect:rect];
UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
[backgroundImage drawInRect:rect];
}
After you can subclass UINavigationController and change initWithRootViewController:
- (id) initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self)
{
CustomNavigationBar *navBar = [CustomNavigationBar new];
[self setValue:navBar forKey:#"navigationBar"];
}
return self;
}
Also you can vary this approach by making Category for UINavigationController and implementing method swizzling for initWithRootViewController:
P.S. Yesterday my new app appeared at AppStore without any problem with this approach.