How can I change the color of navigation bar from its default blue color?
Thanks.
The UINavigationBar class has a UIColor *tintColor property that you can change in code.
Alternately this property is also exposed in the InterfaceBuilder UI design tool.
Assuming you have added the navigation bar programmatically and not in Interface Builder, just put this in your viewDidLoad method.
self.navigationController.navigationBar.tintColor = [UIColor grayColor];
TintColor property doesn't affect default subview of navigation bar as bottom border and shadow. Sometimes it's useful to override layout of navigation bar at all.
Despite navigationBar is read-only property for UINavigationController you can avoid
this restriction by "setValue:forKey:". This method was approved on 5 applications successfully submitted to AppStore.
You can subclass UINavigationBar and change drawRect: method as you want.
For example,
#implementation CustomNavigationBar
- (void) drawRect:(CGRect)rect
{
[super drawRect:rect];
UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
[backgroundImage drawInRect:rect];
}
After you can subclass UINavigationController and change initWithRootViewController:
- (id) initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self)
{
CustomNavigationBar *navBar = [CustomNavigationBar new];
[self setValue:navBar forKey:#"navigationBar"];
}
return self;
}
Also you can vary this approach by making Category for UINavigationController and implementing method swizzling for initWithRootViewController:
P.S. Yesterday my new app appeared at AppStore without any problem with this approach.
Related
[[[UINavigationBar appearance] setTitleView:button]
I tried to set title view as button for all navigation item using above line of code but it is not working.How can I set title view for all navigation bars in my project by writing small code in appdelegate ?
Customizing the Appearance of a Navigation Bar
it is alright to modify the barStyle, tintColor, and translucent
properties, but you must never directly change UIView-level properties
such as the frame, bounds, alpha, or hidden properties directly.
For more detail you can follow apple doc UINavigationBar Class Reference
Edit ->
I solved your query
[[UINavigationBar appearance] addSubview:yourView];
Other Navigation Related query
try this dude...
//put whatever object in this view..for example button that you want to put...
UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds];
ctrl.backgroundColor = [UIColor yellowColor];
ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[navController.navigationBar addSubview:ctrl];
let me know it is working or not!!!!
Happy Coding!!!
This is how you create a navigation controller in addDelegate and set a title to it,
you have to add the following code to didFinishLaunchingWithOptions method.
Notice that you need to set a root view for the viewController which will the viewController that will be displayed inside the navigationController.
yourViewController *mainVC = [[yourViewController alloc]init];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:mainVC];
navigationController1.title = #"title";
[window addSubview:navigationController1.view];
If you use iOS4, you can override drawRect
#implementation UINavigationBar (UINavigationBarCategory)
-(void)drawRect:(CGRect)rect
{
UIImageView *itleImageView = //create object
[self addSubView:titleImageView];
//set title view in navigation bar
}
#end
iOS 5,
if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
UIImageView *itleImageView = //create object
[self.navigationController.navigationBar addSubView:titleImageView];
}
}
i guess this is a right, as i don't have implemented.
I wanted to add a logo in every NavigationController, so I made a subclass of
UINavigationController and used this in the viewDidLoad-Method
UIImage *logo =[UIImage imageNamed:#"logo"];
CGFloat navWidth = self.navigationBar.frame.size.width;
CGFloat navHeight = self.navigationBar.frame.size.height;
UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(navWidth-logo.size.width - 5,
(navHeight - logo.size.height) / 2,
logo.size.width,
logo.size.height)];
logoView.image = logo;
[self.navigationBar addSubview:logoView];
I have my interface completely laid out with Storyboards (including the UITabBarController and all corresponding views).
Now comes the time to customize the Tab Bar. Since I have icons that are already set to the correct colour, I can't use [[UITabBar appearance] setTintColor:] (it just keeps looking wrong).
Turns out I'm supposed to use setFinishedSelectedImage:withFinishedUnselectedImage: on the specific UITabBarItem's.
Is it possible to use this method from the AppDelegate (where the rest of my global customization occurs)? How does the AppDelegate know which UITabBar to target?
If instead, I'm supposed to customize each UITabBarItem from each UIViewController, how do I reference the UITabBar (or "root view controller"?) and then specific item from the UIViewController?
Any help would be greatly appreciated.
Thanks!
In viewDidLoad of your UIViewController instances, you can do
[self.tabBarItem setFinishedSelectedImage: withFinishedUnselectedImage:]
Try this
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"t1s"] withFinishedUnselectedImage:[UIImage imageNamed:#"t1"]];
[self.tabBarItem setTitle:#"Title"];
}
return self;
}
Also, delete image in tabbar item from storyboard view
I'm trying to reduce the brightness of my app at night, and while I have pretty good control over my UIView, the UITabBar and UINavigationController are giving me trouble.
How can I dim UITabBar and UINavigationController them without hiding them?
For the UITabBar you could do:
tabBar.alpha = 0.5
A UINavigationController is not a view, it is a controller, thus it doesn't make sense when you say you want to dim it. If you meant that you want to dim the UINavigationBar, you could do:
navigationController.navigationBar.alpha = 0.5;
Or if you want to dim everything in the navigationController:
navigationController.view.alpha = 0.5;
I would create a subclass of UIView that provides a solid black view. Next, you're going to have to pass touch events through the view so you will need to override -pointInside:withEvent:, return NO and pass the message up to the superview. Insert this view as a subview of the view you're trying to dim. Use the alpha property to control the dimming effect.
Edit I'm bored. Here's something I just threw together.
IADimmingView.h
#import <UIKit/UIKit.h>
#interface IADimmingView : UIView
- (id)initWithContainingView:(UIView *)view;
- (void)dim;
#end
IADimmingView.m
#import "IADimmingView.h"
#interface IADimmingView ()
#property (strong, nonatomic) UIView *containingView;
#end
#pragma mark -
#implementation IADimmingView
#synthesize containingView;
- (id)initWithContainingView:(UIView *)view
{
NSParameterAssert(view);
self = [super initWithFrame:view.frame];
if (!self)
return nil;
containingView = view;
self.backgroundColor = [UIColor blackColor];
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
[self.containingView pointInside:point withEvent:event];
return NO;
}
- (void)dim
{
[self.containingView addSubview:self];
}
#end
In your view controller, it is used like this...
IADimmingView *dimmingView = [[IADimmingView alloc] initWithContainingView:self.tabBarController.tabBar];
dimmingView.alpha = 0.75;
[dimmingView dim];
You may make them custom. This was asked many times here, for example: Custom colors in UITabBar
Just do the same with navigation bar.
This is how the navigationBar is declared in the documentation:
The navigation bar managed by the navigation controller. (read-only)
It is permissible to customize the appearance of the navigation bar using the methods and properties of the UINavigationBar class but you must never change its frame, bounds, or alpha values or modify its view hierarchy directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method.
This is the answer that I used to fix this: Visible buttons with transparent navigation bar
In my tabBar based app I have subclassed the UINavigationBar. Let's say I have three of them: BlueNavBar, BlackNavBar and RedNavBar. It looks something like this:
//BlueNavBar.m
- (void)drawRect:(CGRect)rect {
self.tintColor = [UIColor colorWithRed:65.0f/255.0f green:(156.0f/255.0f) blue:(215.0f/255.0f) alpha:1.0];
UIImage *image = [[UIImage imageNamed:#"blueNavBar.png"]retain];
[image drawInRect:rect];
[image release];
}
I've assigned the subclassed navigationbar for each tab with Interface Builder. That is working great, no problems there.
In some viewControllers however i want to change the navigationBar during "pushViewController". Let's say I want to change the current navigationbar (which is for e.g. BlueNavBar) to the RedNavBar. How can I do this programmatically, without Interface Builder?
It depends on how you've designed the view controller classes themselves. One way to design what you need would be to set the navigation bar type (i.e. colour) when you create the view controller, before you push it on the stack. Something like:
SomeViewController* someViewController = [[SomeViewController alloc] initWithNibName:#"SomeView" bundle:nil];
someViewController.navigationBarStyle = NBStyleRed; // NBStyleRed defined as an enum somewhere
[self.navigationController pushViewController:someViewController animated:YES];
[someViewController release];
The setter method for navigationBarStyle would then (re)create an appropriately-coloured navigation bar for the view controller.
My client can't read iPhone's default fonts, the size is too small. I have an application with a navigation bar and I need to make everything in it bigger, for example, the font's size.
IB doesn't seem to allow this... any help?
Many thanks!
Update: today (2012) there is a much bigger tendency towards custom UIs, so I would say the answer below is way too harsh. There is still no supported way of customizing height, though, but you can certainly derive from UINavigationBar and override some sizing methods. This probably will not get you rejected (although it is still a grey area, just something Apple will probably overlook today).
Once you get the size you want, you can use iOS 5 customization APIs to add the custom background image (see WWDC 2011 Session 114 - Customizing the Appearance of UIKit Controls).
Original answer from 2009:
This is generally impossible.
What's more, I believe making the navigation bar taller is a violation
of Apple Human Interface Guidelines, and your application may be
rejected from the App Store because of it. Please make sure your
client understands this risk before proceeding.
(Pointing out rejection risks is usually a good way to convince
clients against making nonsense decisions.)
Many of the answers here are incorrect, or incomplete, so I wanted to add my answer here in the hope that it might enlighten some.
First off, there is nothing wrong with changing the height of the navigation bar. People commenting saying its not allowed, or goes against the guidelines are simply misunderstanding those guidelines.
The ability to adjust or alter the default navigation bar that's used inside a UINavigationController has been part of the SDK since iOS 5.
- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass NS_AVAILABLE_IOS(5_0);
The easiest way to change the height of the status bar is to use this method when initialising your navigation controller, passing in your custom UINavigationBar sub-class.
TestViewController *t = [[TestViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];
[nav setViewControllers:#[t]];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
Where an example of such a custom UINavigationBar class could look like:
#interface MyNavigationBar : UINavigationBar
#end
#implementation MyNavigationBar
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize s = [super sizeThatFits:size];
s.height = 90; // Or some other height
return s;
}
#end
If you decided to just change the font size in the navigation bar, you can do this (usually in your UIViewController's viewDidLoad method):
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[titleLabel setBackgroundColor:[UIColor clearColor]];
// here's where you can customize the font size
[titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setText:self.title];
[titleLabel sizeToFit];
[titleLabel setCenter:[self.navigationItem.titleView center]];
[self.navigationItem setTitleView:titleLabel];
[titleLabel release];
By subclassing you can achieve that and still support iOS 3+:
Complete example:
#import <UIKit/UIKit.h>
#interface ASNavigationBar : UINavigationBar
#property (nonatomic , retain) UIImage *backgroundImage;
#end
And implementation:
#import "ASNavigationBar.h"
#implementation ASNavigationBar
#synthesize backgroundImage = _backgroundImage;
-(void) setBackgroundImage:(UIImage *)backgroundImage
{
if (_backgroundImage != backgroundImage)
{
[_backgroundImage release];
_backgroundImage = [backgroundImage retain];
[self setNeedsDisplay];
}
}
-(void) drawRect:(CGRect)rect
{
// This is how the custom BG image is actually drawn
[self.backgroundImage drawInRect:rect];
}
- (CGSize)sizeThatFits:(CGSize)size
{
// This is how you set the custom size of your UINavigationBar
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height);
return newSize;
}
#end
Important notes:
If the background image is with transparent areas, you have to set its barStyle property to "translucent" or the transparent areas will be black.
If you have a NavigationBar taller than 44 points, you have to take into account that the position of the BarButtonItems might not be correct. They all will be anchored to the bottom of the bar. you can fix that by overriding layoutSubviews and change their origin.y value.
You should not change the height of the navigation bar. From Apple Programing Guide on View Controller:
Customizing the Navigation Bar Appearance
In a navigation interface, a navigation controller owns its UINavigationBar object and is responsible for managing it. It is not permissible to change the navigation bar object or modify its bounds, frame, or alpha values directly. However, there are a few properties that it is permissible to modify, including the following:
● barStyle property
● translucent property
● tintColor property
(taken from Apple: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html)
UPDATE -- IOS 7 --- still only the available properties can be changed but below is a great tutorial on how to achieve flexibility in the navigation bar http://www.appcoda.com/customize-navigation-status-bar-ios-7/
To add to Skela's answer:
If you initiate your navigation controller in the Storyboard you can change the class of your UINavigationBar in the storyboard to your custom navbar.
and then implement the change height in the class
#interface MyNavigationBar : UINavigationBar
#end
#implementation SwitchAssessmentNavigationBar
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize s = [super sizeThatFits:size];
s.height = 200; // Or some other height
return s;
}
#end