UIAppearance subview disappears from Navigationbar - uiview

I'm doing some changes to the Navigationbar with appearance in appdelegate.
This is my method:
-(void) setAppearance{
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:#"AvantGarde-ExtraLight" size:18] forKey:NSFontAttributeName];
[titleBarAttributes setValue:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:6.0/256.0 green:57.0/256.0 blue:84.0/256.0 alpha:1.0]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
int borderSize = 3;
UIImageView *navBorder = [[UIImageView alloc] initWithFrame:CGRectMake(0,
41,
320,
borderSize)];
navBorder.image = [UIImage imageNamed:#"energy_line"];
navBorder.tag = 999;
[[UINavigationBar appearance] addSubview:navBorder];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
I have a method in my appdelegate that sets the window to my first viewController, when i call this method, my navBorder is removed from the navigationbar. I don't understand why this happens, there is no code that changes anything in my navigationbar in the viewcontroller.
- (void)rootView
{
[self.window setRootViewController:initialViewController];
}

I answered pretty much the same question here: https://stackoverflow.com/a/26414437/538491
But here is a summary:
Calling [[UINavigationBar appearance] returns an appearance proxy for the receiver class. The addSubview: method is not tagged as UI_APPEARANCE_SELECTOR. One major downside to UIAppearance's proxy approach is that it's difficult to know which selectors are compatible.
You should get a hold of the navigation bar and add the image there by calling this method: [self.navigationController.navigationBar addSubview:navBorder] or you should subclass UINavigationBar which gives you more flexibility.

Related

Set Bar Button Items color in app delegate

In my view controller I can set my bar button item like this
[self.navigationItem.backBarButtonItem setTintColor:[UIColor whiteColor]];
But what I want to achieve is to set it in my app delegate so that all my bar button items on all view controllers are white something similar to doing this
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
I can get it to work on text but I want to change it for images
Sample Image,
Edited:
How I set my image
You can use the same strategy to change the UINavigationBar barTintColor and titleTextAttributes:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // this will change the back button tint
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor whiteColor]
forState:UIControlStateSelected];
Edited:
UIImage *selectedImage = [[UIImage imageNamed:#"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// Make in Appdelegate.h
#property(nonatomic)UIColor* appDelColor;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// set AppDelegate Color
appDelegate.appDelColor= [UIColor redColor];
//get AppDelegate Color
UIColor *appDELColor=appDelegate.appDelColor;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
return YES;
}
May this will help you.. :)

DocumentInteractionController Navigation Bar Color

In my iOS application, I am using the DocumentInteractionController to preview the .csv document.
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileLocation];
[self.documentController setDelegate:self];
[self.documentController presentPreviewAnimated:YES];
However, I am finding that the navigation bar is completely transparent. The back button is white, so it is not visible due to the white background.
Note that I have styled the navigation bar in the AppDelegate:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0/255.0f green:138/255.0f blue:188/255.0f alpha:1.0f]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"DINPro-Bold" size:17]}];
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"shadow"]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
Essentially, my question is how can I make the appearance of the navigation bar in the DocumentInteractionController View Controller consistent with the appearance of the navigation bar throughout the entire app (or at least visible!).
This line puts a transparent (or rather void) background image to your UINavigationBar. Why is that?
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
Just remove the line and everything works well.
If you want to set a shadow image, then you should think about using appearanceWhenContainedIn: instead of appearance so it won't spread to unhandled controllers.
As for the Status Bar Style, the simplest way would be to pass self.navigationController as the presenter, instead of self:
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self.navigationController;
}
Hope this will help,

iOS 7 Navigationbar background image issue

I am using Image as Navigation bar background Image. To set Image I used following code:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_logo_ios7.png"] forBarMetrics:UIBarMetricsDefault];
For iOS7 "nav_logo_ios7.png" image size is 768x64 and for iOS6 and bellow I used image has size 768x44.
This is working well on all UIViewControllers.
In same project I am using UIActivityViewController. On iOS7 mail compose view look like this:
How I can handle this?
Thanks in advance.
The issue you are facing is that when a UIViewController is presented modally, the status bar is not included in the height of the UINavigationBar.
This means that the 64pt image is incorrect.
First of all, the official and better way to check what version of iOS the device is running would be to do something like this:
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
//handle iOS 7 Stuff
}
else
{
//handle older iOS versions
}
For more information, check out the NSObjCRuntime.h header.
UINavigationBar background images shouldn't really be a fixed size image and instead should be stretchable image such as a repeatable pattern so maybe it would be an idea to rethink future designs... However if you do want to continue with a custom fixed sized image then I have a suggestion for you...
The UINavigationController allows you to initialise an instance with custom UINavigationBar and UIToolbar classes using initWithNavigationBarClass:toolbarClass:... This means that you could init any views that you are not presenting modally with a different UINavigationBar subclass to views that are being modally presented.
This means that you will be able to specify different background images dependant on if your navigation controller is modally presented or not, for example:
UIImage *backgroundImage44pts = [UIImage imageNamed:#" ... "];
UIImage *backgroundImage64pts = [UIImage imageNamed:#" ... "];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
//handle iOS 7 Stuff
[[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault];
[[UINavigationBarSubclass appearance] setBackgroundImage:backgroundImage64pts forBarMetrics:UIBarMetricsDefault];
}
else
{
//handle older iOS versions
[[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault];
}
One important thing to note is that the MFMailComposeViewController isn't a real view controller so trying to initialise it with custom navigation bar subclasses may not work.. That is why I have used a custom navigation bar subclass for all non-modal navigation controllers and not the other way round.
Another thing to note would be that if your application is universal then modal views do not exist (unless you have anything custom) and you would not have to worry about this.
As I said earlier... UINavigationBars aren't really designed to have fixed sized background images (this is why it is so difficult to achieve) so if you think this work around is too complicated then maybe it would be a good idea to rethink your design.
And one last thing (I promise)... One of the main design changes in iOS 7 is to have your content from the navigation bar flowing underneath the status bar.. Adding an image to prevent this and replace it with a solid white background seems rather strange for an iOS 7 app.
//In `AppDelegate.m`
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
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];
}
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}

Navigation Bar Appearance Settings don't apply constantly

I tried to customise my UINavigationBarController:
// Customize NavBar Appearance
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:#"Lato-Light" size:35.0]}];
// Set NavBar Bottom Border to White by adding a view with height 1
UINavigationController *navBarController = [[self.tabBarController viewControllers] objectAtIndex:0];
CGFloat navBarWidth = navBarController.navigationBar.frame.size.width;
UIView *navBottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, kNavBarHeight- 1, navBarWidth, 1)];
[navBottomBorder setBackgroundColor:[UIColor colorWithWhite:255.0f alpha:0.25f]];
[navBottomBorder setOpaque:YES];
[[UINavigationBar appearance] addSubview:navBottomBorder];
The problem I encounter is, that this added subview randomly disappears when clicking through my tab bar items. Sometimes it is visible perfectly, sometimes only the appearance settings for text and font color are presented. By the way: These are always correct. Only my subview is not showing up anymore after the first boot and switching the tabs.
When I use "insertSubview" instead of "addSubview" it seems to work. So I could imagine, that the navbar is being drawn over my added subview?

Change single navigation bar color for total app

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 ..."

Resources