Navigation Bar Appearance Settings don't apply constantly - uinavigationbar

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?

Related

Change item color in UITabBar iOS

My app has a Tab Bar like the follow:
The Tab Bar must be green and as you can see the icon of the item in Tab Bar are a little hard to see. How I can change the color of the icon in this Tab Bar? I've to use the standard Tab Bar.
Thank you
try this
[[self tabBar] setSelectedImageTintColor:[UIColor greenColor]];
or this
[UITabBarItem.appearance setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected];
Or this
[[self tabBar] setTintColor:[UIColor redColor]];
or this
[UITabBarItem.appearance setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected];
Instead using of icon you can use icon image by using the following code
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
tabBarItem1.selectedImage = [[UIImage imageNamed:#"selectedImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:#"unselectedImage.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.imageInsets= UIEdgeInsetsMake(6, 0, -6, 0);
Try this
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];
// set tabbar background image
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbar_bg"]];
// remove shadow image of tabbar
[[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
I would suggest, to change background of UITabbar to some darker colours, instead of green. Just leave the default gray color(Apple masks to gray) for unselected tab. Use below snipped to set selected tab mask color.
[[UITabBar appearance] setTintColor:[UIColor redColor]];
This will avoid confusion to end users whether a tab is selected or in unselected state.

Remove Navigation Bar shadow Colour on iPhone 6/6plus

I'm trying to remove this white stroke. I've previously fixed this using
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
and it work perfectly on iPhone 4s,5,5s and iOS 7/8. But for some reason on iPhone 6/6+ that whte shadow is still there.
I've also tried this to no result. It just makes the shadow a darker colour than my current navBar bgColour (white: 0 alpha 0.9).
UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.height, self.navigationController.navigationBar.width, 1)];
bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.9];
[self.navigationController.navigationBar addSubview:bgView];
[self.navigationController.navigationBar setShadowImage:nil];
Any ideas?
Try doing the following, this at least works for me.
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
I also then set a custom colour
[[UINavigationBar appearance] setBarTintColor:[UIColor XXXXXX]];
Try to set navigation's bar property clipsToBounds to YES

How to make UINavigationBar fully transparent or gone like this only title and UIBarButtonItem could be seen

As you can see, the UINavigationBar is fully transparent. Only the title and UIBarButtonItem is visible. And the status bar has the same colour as UITableView's background colour. Right now, I have finished to make the table view and cell has the same effect as the pic. But how to make the navigation bar and the status bar has the effect too?
Try this one
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
Try this !
[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
[self.navigationController.view setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];

UIAppearance subview disappears from Navigationbar

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.

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;
}

Resources