uinavigationbar background image hides title - ios

When i add background image to uinavigationbar it hides my title on uinavigationbar,
Here is my code,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed: #"BarBg.png"] forBarMetrics:UIBarMetricsDefault];
}
Code in Class:
int height = self.navigationController.navigationBar.frame.size.height;
int width = self.navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
navLabel.text=#"Categories";
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.font = [UIFont boldSystemFontOfSize:17];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;
After writing this code i am unable to see title on navigation bar, title is behind navigationBar image.
Please help, Thanks in advance..

instead of set label as a titleView of UINavigationBar add that UILabel as a Subview of NavigationBar like bellow...
[self.navigationController.navigationBar addSubview:navLabel];
[self.navigationController.navigationBar bringSubviewToFront:navLabel];

If you're looking only to customize your navigationBar title, I recommend using the appearance instead of adding a label to your navbar.
[[UINavigationBar appearance] setTitleTextAttributes:#{
UITextAttributeFont: [UIFont fontWithName:yourFontName size:fontSize],
UITextAttributeTextColor :[UIColor whiteColor],
}];

Related

Autoshrink Navigation Bar Title iOS 7 or Later

I was wondering what is the best approach to make the UINavigationBar title auto shrink into minimum font size when it is too long?
Below are my example of the problem
Normal
Too Long (need to shrink the label)
And here is the codes I am using to do styling for UINavigationBar
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor greenColor]]
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [fontWithName:#"Helvetica-Bold" size:18.0]}];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];
The most common solution is to use a custom view for the title. But you can't do this using UIAppearance, you will need to do this on each UIViewController that you push, or make a category of the UINavigationBar, and add this logic every time the title text is changed (Not sure if this will work or if it's a good idea, though) or create a new adhoc method.
For example (Put this in the viewDidLoad of your UIViewController):
UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
titleLabel.text = #"Your very long title";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.adjustsFontSizeToFitWidth = YES; // As alternative you can also make it multi-line.
titleLabel.minimumScaleFactor = 0.5;
self.navigationItem.titleView = titleLabel;

Navigation Bar bar appearance

how do you change you navbar placeholder text color? also is there a light blue color for these messages?
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
Think this will work.
- (void)viewDidLoad
{
[super viewDidLoad];
//I am using UIColor BlueColor for an example but you can use whatever color you like
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor blueColor]};
//change the title here to whatever you like
self.title = #"Home";
}

Custom navigation bar minimum font scale?

I need to set a minimum font scale on my navigation bar. I set title attributes in my AppDelegate.m:
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor colorWithRed:54/255.0f green:54/255.0f blue:54/255.0f alpha:1.0f],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 0.0f)],
UITextAttributeFont: [UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0f]
}];
My navigation bar is dragged and dropped on a UIView in my storyboard. Everything works fine with cusom navigation bar image and the text attributes, except I need to set the minimum font scale if it's possible.
I solved it by creating a label and use as navigation bar title.
// NavBar title
UILabel *customNavBarTitle = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 320, 44)];
customNavBarTitle.textAlignment = NSTextAlignmentCenter;
customNavBarTitle.adjustsFontSizeToFitWidth = YES;
[customNavBarTitle setFont:[UIFont fontWithName:#"OpenSans-Semibold" size:15.0f]];
[customNavBarTitle setBackgroundColor:[UIColor clearColor]];
[customNavBarTitle setTextColor:[UIColor blackColor];
[customNavBarTitle setText:customTitle];
[self.view addSubview:customNavBarTitle];

iOS 7 UIBarButton back button arrow color

I'm trying to change the back button arrow
I'm currently using the following to control the text size as well as the text color on the back button:
[[UIBarButtonItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont boldSystemFontOfSize:16.0f], UITextAttributeFont,
[UIColor darkGrayColor], UITextAttributeTextShadowColor,
[NSValue valueWithCGSize:CGSizeMake(0.0, -1.0)], UITextAttributeTextShadowOffset,
nil] forState:UIControlStateNormal];
but if I want to change only the arrow's color for the back button, what should i do?
To change the back button chevron color for a specific navigation controller*:
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
*If you are using an app with more than 1 navigation controller, and you want this chevron color to apply to each, you may want to use the appearance proxy to set the back button chevron for every navigation controller, as follows:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
And for good measure, in swift (thanks to Jay Mayu in the comments):
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
You have to set the tintColor of the entire app.
self.window.tintColor = [UIColor redColor];
Or in Swift 3:
self.window?.tintColor = UIColor.blue
Source: iOS 7 UI Transition Guide
You can set the color on the entire app navigation's bar using the method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions{
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}
It is possible to change only arrow's color (not back button title's color) on this way:
[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor blackColor]];
Navigation bar contains subview of _UINavigationBarBackIndicatorView type (last item in subviews array) which represents arrow.
Result is navigation bar with different colors of back button arrow and back button title
If you are using storyboards you could set the navigation bar tint colour.
Inside the rootViewController that initializes the navigationController, I put this code inside my viewDidAppear method:
//set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
//set back button arrow color
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
In iOS 6, tintColor tinted the background of navigation bars, tab bars, toolbars, search bars, and scope bars. To tint a bar background in iOS 7, use the barTintColor property instead.
iOS 7 Design Resources iOS 7 UI Transition Guide
You can set the tintColor property on the button (or bar button item) or the view controller's view. By default, the property will inherit the tint from the parent view, all the way up to the top level UIWindow of your app.
I had to use both:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor whiteColor]];
And works for me, thank you for everyone!
UINavigationBar *nbar = self.navigationController.navigationBar;
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
//iOS 7
nbar.barTintColor = [UIColor blueColor]; // bar color
//or custom color
//[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
nbar.navigationBar.translucent = NO;
nbar.tintColor = [UIColor blueColor]; //bar button item color
} else {
//ios 4,5,6
nbar.tintColor = [UIColor whiteColor];
//or custom color
//[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
}
Update Swift 3
navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.yellow
navigationController?.navigationBar.tintColor = UIColor.red
navigationController?.navigationBar.barTintColor = UIColor.gray
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blue]
Result:
Just to change the NavigationBar color you can set the tint color like below.
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
In case you're making custom back button basing on UIButton with image of arrow, here is the subclass snippet.
Using it you can either create button in code or just assign class in Interface Builder to any UIButton.
Back Arrow Image will be added automatically and colored with text color.
#interface UIImage (TintColor)
- (UIImage *)imageWithOverlayColor:(UIColor *)color;
#end
#implementation UIImage (TintColor)
- (UIImage *)imageWithOverlayColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
if (UIGraphicsBeginImageContextWithOptions) {
CGFloat imageScale = 1.0f;
if ([self respondsToSelector:#selector(scale)])
imageScale = self.scale;
UIGraphicsBeginImageContextWithOptions(self.size, NO, imageScale);
}
else {
UIGraphicsBeginImageContext(self.size);
}
[self drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#end
#import "iOS7backButton.h"
#implementation iOS7BackButton
-(void)awakeFromNib
{
[super awakeFromNib];
BOOL is6=([[[UIDevice currentDevice] systemVersion] floatValue] <7);
UIImage *backBtnImage = [[UIImage imageNamed:#"backArrow"] imageWithOverlayColor:self.titleLabel.textColor];
[self setImage:backBtnImage forState:UIControlStateNormal];
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
[self setImageEdgeInsets:UIEdgeInsetsMake(0, is6?0:-10, 0, 0)];
}
+ (UIButton*) buttonWithTitle:(NSString*)btnTitle andTintColor:(UIColor*)color {
BOOL is6=([[[UIDevice currentDevice] systemVersion] floatValue] <7);
UIButton *backBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
UIImage *backBtnImage = [[UIImage imageNamed:#"backArrow"] imageWithOverlayColor:color];
[backBtn setImage:backBtnImage forState:UIControlStateNormal];
[backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, is6?5:-5, 0, 0)];
[backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, is6?0:-10, 0, 0)];
[backBtn setTitle:btnTitle forState:UIControlStateNormal];
[backBtn setTitleColor:color /*#007aff*/ forState:UIControlStateNormal];
return backBtn;
}
#end
If you want to change only the Back Arrow BUT on the entire app, do this:
[[NSClassFromString(#"_UINavigationBarBackIndicatorView") appearance]
setTintColor:[UIColor colorWithHexString: #"#f00000"]];
In iOS 7, you can put the following line of code inside application:didFinishLaunchingWithOptions: in your AppDelegate.m file:
[[UINavigationBar appearance] setTintColor:myColor];
Set myColor to the color you want the back button to be throughout the entire app. No need to put it in every file.
Swift 2.0: Coloring Navigation Bar & buttons
navigationController?.navigationBar.barTintColor = UIColor.blueColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
In swift 3 , to change UIBarButton back button arrow color
self.navigationController?.navigationBar.tintColor = UIColor.black

removing uinavigationbar title margins

I modified my navigation bar as follows in app delegate:
NSDictionary *settings = #{
UITextAttributeFont : [UIFont fontWithName:#"impact" size:36.0],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor : [UIColor clearColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero]};
[[UINavigationBar appearance] setTitleTextAttributes:settings];
But the font cuts down like this for the larger font. :
I tried doing this in viewWillAppear of my VC, :
UIView *newTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[newTitleView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar addSubview:newTitleView];
Then I was going to align title to center. But that just doesn't seems right. Only thing I actually need is to remove margins at top and bottom of the title Label. How do I go about it.
Try this code and don't set setTitleTextAttributes
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
self.navigationController.navigationItem.titleView = titleView;
[titleView release];
}
titleView.text = title;
[titleView sizeToFit];
Well the easiest way i would suggest is to use a UIView with label and button customised for the requirement and use it,not on navigation bar.
mnavigation bar height customization is the trouble that the height is fixed and the subview also it may affect .So I suggest to go with a custom header view subclassed with UIView
You can subclass to the UILabel and override drawTextInRect: like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
For more useful link you can find
here

Resources