Adding colour to uinavigationbar - iphone

How to add colour to the navigation bar ? I need to implement this for iOS 4 and above.
I want a colour similar to the one in the screenshot:

In the example you posted, It looks more like a backround image because of the slight lighting effect. (It's brighter in the middle, it you look closely. In IB, I have the nav bar set to Black Transparent with the following Background Image. (320 x 44px)
h file.
IBOutlet UINavigationBar *navBar;
m file "viewDidLoad" function:
navBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"nav"]];

In iOS < 5 you can set tint color of individual navigation bar directly, e.g.:
// self is a UIViewController
[self.navigationController.navigationBar setTintColor:[UIColor purpleColor]];
Once you don't need compatibility with iOS 4.x you can use appearance proxy for UINavigationBar. This would work for all navigation bars in the application:
[[UINavigationBar appearance] setTintColor:[UIColor purpleColor]];

Related

iOS7 navigationBar and TabBar Color is behaving strangely

This is what I want. It loads on some of my view controllers.
Hi all,
I am going nuts trying to make the tint color of all of my viewControllers the same. Some appear to be much darker than others. All I want is the light color to be throughout...
Sometimes I get this ugly dark gray instead... I am not sure what I am doing incorrectly. I have checked the .m file and am not setting the tint color or anything... not sure why it wouldnt be consistent on every viewController...
Any help would be great. Thanks!
in iOS7 navigation bar is by default translucent=YES so just change to NO like bellow:-
self.navigationController.navigationBar.translucent=NO;
and set Navigaitonbar color or other property customize like Bellow put this code into Appdelegate class didFinishLaunchingWithOptions and use appearance for applying Globally:-
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
[[UINavigationBar appearance]setTintColor:NavigationColor];
} else {
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]]; // it set color of bar button item text
[[UINavigationBar appearance]setBarTintColor:[UIColor GreenColor]]; // it set color of navigation
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault]; // it set Style of UINavigationBar
[[UINavigationBar appearance]setTitleTextAttributes:#{UITextAttributeTextColor : [UIColor whiteColor]}]; //It set title color of Navigation Bar
// Load resources for iOS 7 or later
}
For tabBar also same this is by default translucent=YES change to NO
[self.tabBarController.tabBar setTranslucent:NO];
A common mistake is setting the view.backgroundColor of the View Controller to clearColor (both programmatically or via Storyboard). This makes the view actually black instead (since there's nothing beneath the clear view), so everything that is above that view, that has translucent property set to YES, will show dark grey color (black color + default iOS blur).
To fix this, either set the translucent property to NO (as Nitin Gohel said), or set the view.backgroundColor to white, which's its actual default color.
Hope this still helps someone!
Since iOS 7.1 there's a bug, which causes UITabBar to not listen to Global Tint.
See this post: https://stackoverflow.com/a/22323786/1255674
You need to set the tint programmatically. Thanks, Ive ...

How to get a fully transparent TabBar in UITabBarController [duplicate]

This question already has an answer here:
UITabBar fully transparent
(1 answer)
Closed 9 years ago.
I have spent the last few hours trying to get the TabBar in a UITabBarController fully transparent (clear background). I use a custom subclass of the UITabBarController and I managed to change the tintColor, the alpha, but the background color definitely remains the one defined in IB.
Thanks for your help, I'm getting crazy...
Here is one way to accomplish that.
CGRect frame = CGRectMake(0.0, 0.0, 320, 48);//Setting a Frame.
myTabView = [[UIView alloc] initWithFrame:frame];//Making Tab View
// not supported on iOS4
UITabBar *tabBarr = [self.tabBar tabBar];
if ([tabBarr respondsToSelector:#selector(setBackgroundImage:)])
{
// set it just for this instance
[tabBarr setBackgroundImage:[UIImage imageNamed:#"hot-1.png"]];
// set for all
// [[UITabBar appearance] setBackgroundImage: ...
}
else
{
// ios 4 code here
//[tabBarr setBackgroundColor:c];
}
//[myTabView setBackgroundColor:c];//Setting Color Of TaBar.
[myTabView setAlpha:0.8];//Setting Alpha of TabView.
[[self.tabBar tabBar] insertSubview:myTabView atIndex:0];//Inserting Tab As SubView.
And here is a link to a tutorial I found very useful in changing the tab bar's background. You can play around with the code and costomize it to your liking.
http://ios-blog.co.uk/tutorials/how-to-customize-the-tab-bar-using-ios-5-appearance-api/
Edit:
As far as setting the background color of tabbar to transparent or clear color, you have two ways. One is the image which I explained and you didn't like. The other one is to set the background of tabbar in its supper view. Something along the following line.
tabBar.superview.backgroundColor = [UIColor clearColor];
This way you reach behind the tabbar and change that black background to whatever you want.
Edit 1:
sorry for delay in adding the solution for ios 7. below is the method you need to change the background color of the tabbar in ios 7.
just add the following line of code inside your ViewDidLoad method and it will do the magic.
[self.tabBarController.tabBar setBackgroundColor:[UIColor greenColor]];
hope this helps.:)

UINavigationBar handle rotation

I'm trying to handle the rotation of a UINavigationBar with UINavigationItem (i'm not using the UINavigationController), i have successfully made so that the height and width show according, but that does not seems to be correct, as the UINavigationBar still acts as being in portrait mode (big title and button), also when i set a custom background for both metrics, it seems to only show the portrait background metric, ignoring completely the landscape one.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"modal-top-landscape"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"modal-top"] forBarMetrics:UIBarMetricsDefault];
Is there a way to tell the UINavigationBar that is rotated and should show the text and buttons accordingly?
I attach some screenshots:
One workaround for this is to use a UINavigationController, and use a CGRectOffset to remove the margin that leaves the status bar... add the Controller to the parent controller (also the view).

UITabBar fully transparent

In XCode I am using the interface builder (StoryBoard) to lay out most of my layout. However I want some custom drawing on this. This works quite well.
There is however a problem what I am facing. I have a "bite" out of the active tab. (see http://cl.ly/Efno ) I want this bite fully transparent. (I have set an pink background color to see what part I want transparent which is not transparent.)
How I have changed the look and feel is the following.
Set the UITabBar class to my own class in the interface builder for the corresponding tabbar.
In the awakeFromNib of that class I have set the label position and image and selected image of each tabbar item. Like so
[tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:image];
Each image fully covers the entire tabbar in height and has the width of the tab item itself.
Set the background image of the tabbar to none (a fully transparent image)
Set the background color of the tabbar to a fully transparent color (now I have a pink color set to see where it goes wrong)
In the interface builder uncheck "opaque" for the tabbar.
However, it is not transparent, the pink part is black. How can I make this transparent?
Thanks
Have a look at the appearance proxy for UITabBar, you might be able to do what you want there without having to use a custom subclass. There are tons of properties you can access and change. You could set the relevant properties in the app delegate. It is iOS5 only though, but I gather that you are using that already, since you mentioned storyboards.
E.g.
UIImage *tabBarBackground = [UIImage imageNamed:#"tabBarBackground.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:127.0/255.0 green:186.0/255.0 blue:235.0/255.0 alpha:1.0]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabBarItemSelected.png"]];

Why is this bar button black and not clear?

I'm trying to set the tintColor of a UIBarButtonItem in method that gets called when my program starts. I set up all my views using storyboards. I then customize the appearance of the views using the new iOS5 guidelines for using appearance proxies. I've customized the background of the navigation bar by doing the following:
- (void)customizeAppearance
{
UIImage *leatherTexture = [[UIImage imageNamed:#"BrownLeather#2x.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:leatherTexture
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:leatherTexture
forBarMetrics:UIBarMetricsLandscapePhone];
[[UIBarButtonItem appearance] setTintColor:[UIColor clearColor]];
}
I was hoping that by setting the UIBarButtonItem tintColor to clear would allow me to easily use the default button styles while having a custom background texture. However, setting the tintColor to clear just turns the button black as opposed to being transparent or clear. Any ideas what I'm doing wrong? Is there a way to create a clear button without having to use custom images for the buttons? See the image below:
You can't do this way (because IMO the default background of UIBarButtonItem is black. and new tint color is over-layered on it).
However you can customize your UIBarButtonItem with using UIButton (with background) as customView in UIBarButtonItem.
Or if you are targeting iOS 5 only you can use brown tint color (which will be flat and will not show background image)
Just found out that in Xcode 4.5 you can just drag a UIButton into the UIBarButton in the Storyboard. The UIButton is then fully customizable.