Change single navigation bar color for total app - ios

I have using two UINavigationBar's in my app ,now am applying themes to my app i'm trying to change navigation bar color of the total app using this line of code
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
am getting two navigation bars with black color is there any way to change single navigation bar color for total app

Instead of [UINavigationBar appearance] you can use [UINavigationBar appearanceWhenContainedIn:...] to be more specific about which UINavigationBars are changed based on their context.
For example you could create a subclass of UINavigationController called MyNavigationController (no need to add any behaviour) then do:
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationController class], nil] setTintColor:[UIColor blackColor]];
Only UINavigationBars that live within your subclass will have their appearance changed.
I've used this idea in an iPad app where I wanted UINavigationBars that appeared within modal FormSheets to look different to other UINavigationBars within the app for example.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if(isiPhone5)
{
self.LoadinFirst = [[LoadingFirstViewController alloc] initWithNibName:#"LoadingFirstView-iPhone5" bundle:nil];
}
else {
self.LoadinFirst = [[LoadingFirstViewController alloc] initWithNibName:#"LoadingFirstView" bundle:nil];
}
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.LoadinFirst];
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[navigationController.navigationBar setTintColor:[UIColor colorWithRed:192/255.00f green:182/255.00f blue:184/255.00f alpha:1.0f]];
self.window.rootViewController =navigationController;
[self.window makeKeyAndVisible];
return YES;
}

If you want to put an image in the navigation bar with the color you want you can use
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] )
{
UIImage *image = [UIImage imageNamed:#"titlebg.png"] ;
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
else you can easilt set the tint by using
[navigationController.navigationBar setTintColor:[UIColor colorWithRed:102/255.00f green:182/255.00f blue:114/255.00f alpha:1.0f]];
hope this helps.

To change the over all navigation bar color you can use the method
[[UINavigationBar appearance] setBackgroundColor:[UIColor blueColor]];
To change the overall font and such you can use something like :
-(void) changeNavigationBarStyle{
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"ChalkboardSE-Bold" size:0.0],
UITextAttributeFont,
nil]];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
[attributes setValue:[UIFont fontWithName:#"ChalkboardSE-Bold" size:0.0f] forKey:UITextAttributeFont];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
EDIT DUE TO COMMENTS
To change the color (blue in this example) of all UINavigationBars in the application you can call
[[UINavigationBar appearance] setBackgroundColor:[UIColor blueColor]];
To change the color of one UINavigationBar you can call
self.navigationController.navigationBar.tintColor = [UIColor blueColor];
There is no middle ground where you can say "Change all UINavigationBar's color except ..."

Related

iOS PopoVer NavigationBar don't show BarTintColor

In our app we're setting the navigationbar and toolbar color with the following code. With iOS 8.x this is working fine. Recently I have tested the app with iOS 7.x and the colors aren't shown. The bars are transparent and the text has white color (so this worked...)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:255.0/255.0 green:167.0/255.0 blue:0.0/255.0 alpha:1.0]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UIToolbar appearance] setBarTintColor:[UIColor colorWithRed:255.0/255.0 green:167.0/255.0 blue:0.0/255.0 alpha:1.0]];
[[UIToolbar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont boldSystemFontOfSize:19.0], NSFontAttributeName,
nil]];
I hope somebody could explain me how to solve this. Setting the bars background color is no solution to me because i would use transparency.
The solution to this issue is to set the color-values manually in the popping-up ViewController again for iOS 7.
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar setTintColor:[UIColor whiteColor]];
[self.navigationController.toolbar setBarTintColor:[UIColor colorWithRed:255.0/255.0 green:167.0/255.0 blue:0.0/255.0 alpha:1.0]];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:255.0/255.0 green:167.0/255.0 blue:0.0/255.0 alpha:1.0]];

navigationBar.barTintColor always black and not possible to change it

The navigationBar.barTintColor in my app is always black, and there is no way I can change it. I checked all classes and I never set it to black, but I do set it to UIColor clearColor. Still, the bar is black. Any suggestions?
Edit:I found out that the problem is with my [UIColor clearColor], when I change it to any other color it changes the color like it should, but clearColor makes it appear black.
Have a look there
Try modifying the Style and Translucent attributes on the navigation bar (top right in image).
If you are having problems modifying the status bar color, try adding this to your .plist (line below).
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Finally, here's some code you may want.
// Status bar color
[[UIApplication sharedApplication] setStatusBarStyle:yourStyle];
// Navigation bars color
[UINavigationBar appearance].barStyle = yourStyle;
[UINavigationBar appearance].barTintColor = [UIColor yourColor];
// Navigation bars items color
[UINavigationBar appearance].tintColor = [UIColor yourColor];
If its IOS7 try the code below
[[UINavigationBar appearance]setBarTintColor: [<Specify the UIColor you want>];
In IOS6 try this
[[UINavigationBar appearance] setTintColor: [<Specify the UIColor you want>];
Edit:
I think you have given
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
This will give black color. If you want any specific tint color, it must be specifed after clearing
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
self.navigationController.navigationBar.barTintColor = [UIColor <specify your color>];
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:
#{
UITextAttributeTextColor: [UIColor whiteColor],UITextAttributeTextShadowColor: [UIColor clearColor],UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],UITextAttributeFont: [UIFont fontWithName:#"ArialMT" size:18.0f]
}];
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
// Uncomment to change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#""]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#""]];
// Uncomment to change the font style of the title
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:#"ArialMT" size:18.0], NSFontAttributeName, nil]];
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
}
In iOS 7 try:
[self.navigationController.navigationBar setTranslucent:NO];

MFMailComposeViewController bar background color not changing in iOS7

I'm trying to change the background color of the MFMailComposeViewController in iOS7 but I cannot make it work.
I'm using the following snipped:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
if([picker.navigationBar respondsToSelector:#selector(barTintColor)]) {
// iOS7
picker.navigationBar.barTintColor = READER_NAVIGATION_BAR_BACKGROUND_COLOR;
// Set back button arrow color
[picker.navigationBar setTintColor:READER_NAVIGATION_BAR_BACK_BUTTON_ARROW_COLOR];
// Set Navigation Bar Title Color
[picker.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:READER_NAVIGATION_BAR_TITLE_NORMAL_FONT_COLOR forKey:UITextAttributeTextColor]];
// Set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:READER_NAVIGATION_BAR_BUTTONS_FONT_COLOR, UITextAttributeTextColor,nil] forState:UIControlStateNormal];
}
Does anybody knows how to change the bakcground color of the MFMailComposeViewController in iOS7?
The trick here is to call 'appearance methods' such as
[UINavigationBar appearance].barTintColor = [UIColor whiteColor];
[UINavigationBar appearance].tintColor = [UIColor redColor];
BEFORE calling to
[[MFMailComposeViewController alloc] init];
This way the color scheme will be applied to the mail composer.
It may be returned back to defaults in mailComposeController:didFinishWithResult:
try this. worked for me.
MFMailComposeViewController* myailViewController = [[MFMailComposeViewController alloc] init];
// set other attributes of mailcomposer here.
myMailViewController.mailComposeDelegate = self;
[myMailViewController.navigationBar setTintColor:[UIColor whiteColor]];
[self presentViewController:myMmailViewController animated:YES completion:nil];
Swift 3 solution:
extension MFMailComposeViewController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
}
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.isTranslucent = false
navigationBar.isOpaque = false
navigationBar.barTintColor = UIColor.white
navigationBar.tintColor = UIColor.white
}
}
For iOS8:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Or
navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor];
try this but one thing BarTintColor available only iOS7
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
This color is made translucent by default unless you set the translucent property to NO.
or try this link it will more helpful you
Changing MFMailComposeViewController's toolbar color
Try the following code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];
// Your usual code follows here ......
#SoftDesigner's answer:
As of iOS 9:
[UINavigationBar appearance].tintColor = yourFavoriteColor;
does not work on the MFMailComposeViewController.
The rest of the answer works (I used it), but as far as I can tell you're stuck with Apple's colors for the nav bar buttons.
Hope this saves someone else some angst.
First present the MFMailComposeViewController then change its tintColor
[self presentViewController:emailDialog animated:TRUE completion:nil];
[[UINavigationBar appearance] setBackgroundImage:nil
forBarPosition:UIBarPositionTopAttached
barMetrics:UIBarMetricsDefault];
I had a problem that prevented me from setting the background colour. Turns out I had some other code elsewhere setting the background image to [UIImage new].
The following code fixed it:
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:nil];

MFMailComposeViewController appearance setTintColor getting lost iOS 7

This question is for Xcode 5 running iOS 7 and is super weird. I am trying to set all the UInavigation and UIBarButtonItem text colors to white.
So in my app launch delegate I set the code as.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIImage *NavigationPortraitBackground = [UIImage imageNamed:#"button_header_blue"];
// Set the background image all UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:NavigationPortraitBackground forBarMetrics:UIBarMetricsDefault];
// Set the text appearance for navbar
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Helvetica Neue" size:21], UITextAttributeFont,
nil]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState: UIControlStateNormal];
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
// Override point for customization after application launch.
return YES;
}
If I launch "send mail" action twice
- first time I see UIBarButton items white. I look at it and hit the Cancel button
- second time I see them all of them grayed out and barely visible except for the title.
- This is happening in both my iPhone simulator and iPhone running iOS 7.
how can I fix this?
I had to do it this way in order for it to work on iOS 7
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController.navigationBar setTintColor:[UIColor whiteColor]];
[mailViewController.navigationBar setBarTintColor:[UIColor whiteColor]];
....

iOS 7 UINavigationBar appearance not working first timeā€¦

I am trying to change the look of the UINavigationBar in my iOS7 app. I am doing the following:
- (void)viewDidLoad
{
[super viewDidLoad];
m_sNumberToCall = #"";
UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconHome.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(btHomeTouched:)];
self.navigationItem.leftBarButtonItem = btn;
self.navigationController.navigationBar.translucent = YES;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0],
NSForegroundColorAttributeName,
shadow,
NSShadowAttributeName,
[UIFont fontWithName:#"Helvetica-Bold" size:21.0],
NSFontAttributeName,
nil]];
}
But, the first time I present the UITableViewController it is the standard iOS7 nav bar, then I press home and present it again and it is my new look.
Any ideas why it does not work the first time?
Don't change the appearance but the navigation bar directly. The appearance affects only the future instances but not the already created ones.
Change:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
to:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
The answer before only helps you with the background image but not with the title text attributes.
You don't need to change your code but all you have to do is move it to
applicationDidFinishLaunchingWithOptions
in your AppDelegate.m file.

Resources