Presented UINavigationController tint color not changing - ios

MyVC *vc = [[MyVC alloc] init];
vc.delegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[nc.navigationBar setTintColor:[UIColor redColor]];
[self presentViewController:nc animated:true completion:^{}];
The navigation bar is black. I present this from a navigation controller inside a tab bar controller which is created in my App Delegate. In my ApplicationDidFinishLaunchingWithOptions, I can control the color of the navigation controllers there.
Why is this navigation controller tint bar black?

[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
I would suggest you to use this if all of your NavigationBars are the same color. Appearance property of an object sets uniformity on the function you apply on it.

You can try to make custom class for UINavigationBar
.h
#interface CustomUINavigationBar : UINavigationBar {
}
#end
.m
#implementation CustomUINavigationBar
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor colorWithRed:0.023 green:0.14 blue:0.478 alpha:1];// for example
self.tintColor = color;
}
#end
Also you can try to insert the below code in the didFinishLaunchingWithOptions: of AppDelegate.m
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

In iOS7 you have to use barTintColor property
[nc.navigationBar setBarTintColor:[UIColor redColor]];
instead of
[nc.navigationBar setTintColor:[UIColor redColor]];

Related

Cannot set default background image for MFMailComposeViewController

I'm using a MFMailComposeViewController to compose a mail and add an attachment in my app.
I've got this code in my AppDelegate.m
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0x0A/255.0f green:0x13/255.0f blue:0x1A/255.0f alpha:1]];
Then I'm using this code to create the MFMailComposeViewController in another UIViewController
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
[mailViewController.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
mailViewController.mailComposeDelegate = self;
[self presentViewController:mailViewController animated:YES completion:nil];
However, the UINavigationBar in the MFMailComposeViewController is dark, how come it doesn't "reset" itself to the background I specify before presenting it? Is it inheriting the color of the current UIViewController?
Set UINavigationBar's barTintColor to nil. It will remove the custom tint color.

Different UINavigationBar background for different views

I am creating my UINavigationBar in code like so:
// init and add root navigation controller to view
_rootNavigationController = [[UINavigationController alloc] init];
[_rootView.contentContainer addSubview:_rootNavigationController.view];
_rootNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
Then inside a certain UIViewController I want the UINavigationBar to have a clear background, so i use this code which works:
UINavigationBar *navBar = self.navigationController.navigationBar;
navBar.topItem.title = #"";
[navBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:[UIImage new]];
[navBar setTranslucent:YES];
[navBar setTintColor:[[AVOThemeManager sharedTheme] contentBackButtonColor]];
Once the user leaves this page by pressing back, I want to return my UINavigationBar back to how it origonally looked, so I am trying this:
- (void)viewDidDisappear:(BOOL)animated {
UINavigationBar *navBar = self.navigationController.navigationBar;
[navBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:nil];
[navBar setBarStyle:UIBarStyleBlackTranslucent];
[navBar setTranslucent:YES];
}
But this does not seem to work. I also do not want to set the UINavigationBar to be a new instance, and then set the properties, because it has a global button which is added in the root navigation controller, which the child view controllers don't know about.
Is there anything else I can try?
After posting, I immediately found the answer. Basically instead of using viewDidDisappear, I should use viewWillDisappear and it worked.

How can I add navigationbar background image to MFMailComposeViewController on ios7

I am using MFMailComposeViewController.I can't add navigationBar background image to MFMailComposeViewController on ios7.My Code working ios7 before .But doesn't work on ios7.How can I add navigationbar background image to MFMailComposeViewController on ios7?
MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
mailCompose.mailComposeDelegate = self;
[mailCompose setSubject:#"SubjectName"];
[mailCompose setMessageBody:shareBodyString isHTML:NO];
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)])
{
[self presentViewController:mailCompose animated:YES completion:nil];
}
else {
[self presentModalViewController:mailCompose animated:YES];
}
[mailCompose.topViewController.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationbar-background.png"] forBarMetrics:UIBarMetricsDefault]; // working before ios7
Use the Appearance Proxy. Following code will target the whole application :
UINavigationBar *navBar = [UINavigationBar appearance];
[navBar setBackgroundImage:[UIImage imageNamed:#"navigationbar-background.png"] forBarMetrics:UIBarMetricsDefault];
If you prefer to target only in MFMailComposeViewController change the first line with this :
navBar = [UINavigationBar appearanceWhenContainedIn:[MFMailComposeViewController class], nil];
If you already customised navigation bar and also want it to apply MFMailComposeViewController, It only allows to do it with UIAppearance proxy. For iOS 7.1.1, i replaced navigation bar background but couldn't change status bar background. Also it greyed out bar button items on subsequent calls. Therefore I stop customising and tried to turn back to default navigation bar style before creating MFMailComposeViewController
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
[[UINavigationBar appearance] setTintColor:nil];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont fontWithName:#"Helvetica-Bold" size:18.0], NSFontAttributeName, nil]];
MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init];

IOS7 navigatinBar tintColor change in popover

I developed iPad application. I'm opening some screens in popover with navigation controller. But I did not change navigationcontroller tint color in IOS 7. How can I change this color. thanx
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:airportsSearch] autorelease];
navigationController.navigationBar.barTintColor = [UIColor blackColor];
navigationController.navigationBar.translucent = NO;
self.popOver=[[UIPopoverController alloc] initWithContentViewController:navigationController];
self.popOver.delegate = self;
[self.popOver setPopoverContentSize:CGSizeMake(285, 370)];
[self.popOver presentPopoverFromRect:tempButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
The magical word here is barStyle, you need to do the following if you need it black:
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.navigationBar.translucent = NO;
And if you want to change its color:
navigationController.navigationBar.barTintColor = [UIColor redColor];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.navigationBar.translucent = NO;
Setting the NavigationBarStyle to UIBarStyleBlack also worked for me, but only via the Storyboard.
I tried
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
as well as
[[UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil] setBarStyle:UIBarStyleBlack];
in the didFinishLaunchingWithOptions AppDelegate method. But nothing changed. Only changing the BarStyle of the NavigationControllers NavigationBar inside the Storyboard worked.

Custom transparent UITabBar

I am trying to create an app in which the tab bar transparent (or which looks transparent). For this i'm using part of an image in the view. And set the remaining part as background for tab bar. The problem is the tab bar appears with a darker shade. I'm out of ideas now.
Below is the screen shot
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabbar_bg.png"]];
self.tabBar.backgroundColor=[UIColor whiteColor];
[self.tabBar insertSubview:imgView atIndex:1];
// [self.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"transp.png"]];
// [self.tabBar setSelectedImageTintColor:[UIColor clearColor]];
[[UITabBar appearance] setTintColor:[UIColor clearColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor clearColor]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"transp.png"]];
The above is the code that i've tried out in the viewDidLoad of my custom UITabBarController class. "tabbar_bg.png" is the image used as background for the tab bar and 'transp.png' is a transparent image used as the selectionIndicator image.
Thanks for the help.
Try
[self.tabBar setBackgroundImage:[UIImage new]];
This trick also works for UINavigationBar.
Have you tried creating a subclass of it?
#implementation TabBarInvisible
-(id)init
{
self = [super init];
if (self)
{
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.backgroundImage = nil;
}
return self;
}
-(void)drawRect:(CGRect)rect
{
}
#end

Resources