how to change the color of the tabbar controller - objective-c

Is there any way I can change the Tab Bar Controller's color to something other than the default black? I know this isn't possible in IB, but perhaps maybe through code?

In AppDelegate
self.tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor grayColor];

In AppDelegate.m in didFinishLaunching... method write(this will change for whole app):
[[UITabBar appearance] setBarTintColor:[UIColor myColor]];
Or you can write in ViewController.m in method viewDidLoad:
[self.tabBarController.tabBar setBarTintColor: [UIColor mycolor]];

You can do with XIB/Storyboard as well as programmatically
For Xib/storyboard select tab bar controller than tab and you can see all the options to change tab bar or tab bar view properties see the attached image Image attached here
For programmatically:
for tab bar tint and background
[[UITabBar appearance] setTintColor:panelColor];
[[UITabBar appearance] setBarTintColor:[UIColor lightGrayColor]];

Related

Customizing UINavBar with custom color and without hairline

I want to set my navigation bar to the same color as my UITableViewCell(s), while also hiding the pixel line between the navigation bar and the cells.
The code I've used, in the AppDelegate didFinishLaunchingWithOptions, is this:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:0.465639 green:0.763392 blue:1 alpha:1]];
It removes the pixel line and sets the Nav Bar's color.
However, it leaves the status bar as transparent and my table view appears in the status bar when I scroll down.
How can I add the color to the status bar as well?
Thanks!
If its a solid colour you could always use:
self.navigationController.navigationBar.translucent = NO;
To stop the content going behind the bar
And use the new barTint property
self.navigationController.navigationBar.barTintColor = [UIColor redColor];

UIRectEdgeNone makes NavigationBar and Tabbar darker

I have an iOS 7 app that has a NavigationController inside TabbarController.
I then customize the bars background color
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
[[UITabBar appearance] setBarTintColor:[UIColor blueColor]];
It runs fine. But if there's a ViewController that wants not to be covered by the bars, like this
self.edgesForExtendedLayout = UIRectEdgeTop;
Which means this ViewController does not want to be covered by the Tabbar. But it makes the Tabbar darker than normal
I think this is because I use custom color for the bars. How to fix ?
It probably means that the there's nothing to show below the translucent tab bar. Set the tab bar translucent property to NO
#rounak is right, maybe setting the tab or nav bar's translucency to NO tells iOS not to try to put another tab or nav bar under the current one, which makes it darker.
In the viewDidLoad, add this:
self.navigationController.navigationBar.translucent = NO; // if you have a nav
self.tabBarController.tabBar.translucent = NO; // if you have a tab

UISegmented Control Global Tint in Navigation Controller Header

Setting the global tint in my tableview header isn't tinting as expected, and appears to be a bug.
When I load the view I get
The right hand side retains the default colour, however if I press the right button and the left again it sets to how you would expect it to be:
This problem is only with the segmented control in my navigation bar. The global tinting works when the control is embedded in a "normal" view.
I have a work around which solves the problem. Being, I setting the right control and then back to the left in viewDidLoad.
So is it a bug ? or am i missing something? Thanks.
Add: I am setting the global tint in the storyboard
It's not a bug. Global tinting works for the objects, whose class is used for tinting. If the specific tint scheme is declared for a class, then all the controls belonging to that class will follow the class tint scheme regardless of the view they are added to.
I have tried implementing this & I am here with this code.
Set the global Tinting for UINavigationBar in appDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
...
...
}
and I am adding UISegmentControl in UINavigationBar as well as intableView.tableHeaderView in viewDidLoad method.
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:#[#"One", #"Two"]];
segment.frame = CGRectMake(self.view.frame.size.width/2-75, 0, 150, 40);
[self.navigationController.navigationBar addSubview:segment];
UISegmentedControl *tableSegment = [[UISegmentedControl alloc]initWithItems:#[#"One", #"Two"]];
tableSegment.frame = CGRectMake(self.view.frame.size.width/2-75, 0, 150, 40);
[self.navigationController.navigationBar addSubview:tableSegment];
tableView.tableHeaderView = tableSegment;
This yields following result. The UISegmentControl will be tinted with the UINavigationBar 's global tint if it is added into the UINavigationBar container as SubView, otherwise, it will take the default blue color outside UINavigationBar like in the header of UITableView
Now adding a specific tint color for UISegmentedControl as
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
[[UISegmentedControl appearance] setTintColor:[UIColor brownColor]];
...
...
}
``
It yields this, The UISegmentControl is affected by its global tint now, regardless of its location (like earlier in UINavigationBar & UITableViewHeader). It is
However the TinitColor of UInavigationBar is still applicable on the leftBarButtonItem because there is no global tint scheme declared for the UIBarButtonItem, hence it is adopting the tintColor of the container it is added to.
Adding more code to Clarify
Using global tint for UISegmentedControl & no other global tinting.
[[UISegmentedControl appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
Result: You can see the segment control, but notice the backButton, which is having the default tint of UINavigationBar
Using global tint for UISegmentedControl & UINavigationBar
[[UISegmentedControl appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:39.0/255.0 green:64.0/255.0 blue:112.0/255.0 alpha:1]];
Result: You can see the segment control tint & also notice the backButton, which is having the Global tint of UINavigationBar
I have also done a default selection of UISegmentedControl. If you are getting any other result with this code then there must be some thing extra happening in your code.
I hope that helps.
I've had a big issue with tinting in Xcode 5.0.2 recently. I was playing with both Global Tint and Tint for various items when suddenly the inheritance of tintColor stopped working correctly. I ended up cleaning the Storyboard's XML file manually to fix things out.
If you're not doing any UIAppearance magic in your app, it sure looks like a bug to me.

How can I make my navigation bar uniformly semi-transparent?

I'm writing an app on iOS 7, and I can't seem to get a handle on the transparency of the navigationBar and the toolbar, how can I set the navigation bar to black at 50% opacity?
I've read the transition to ios7 guide and I've watched the wwdc13 lecture 214, but my status bar still has a different transparency than the rest of the attached nav bar.
Here is my code:
// APP-WIDE THEMING
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackOpaque];
[[UINavigationBar appearance] setBackgroundColor:[UIColor blueColor]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Here is the screenshot of my problem: http://grab.by/qiyU
Set the background image to nil, and set the background color with alpha.
[ctrl.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
ctrl.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0 Green:0 Blue:0 Alpha:.5];
According to the answer posted here it's possible to create a transparent UINavigationBar:
How to draw a transparent UIToolbar or UINavigationBar in iOS7
However you want to create a semitransparent navigationbar. For that as far as I can tell you have to create a 1 px large image containing a black color with 50% opacity. Add this as backgroundimage for your Navigationbar.
This snippet should do the trick:
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
UIImage* sti = [UIImage imageNamed:#"EMT_SemiTransparent.png"];
[[UINavigationBar appearance] setBackgroundImage:sti forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:UIColor.clearColor];
try setting key [View controller-based status bar appearance] to NO in your pList file as well.
i've run into some funkiness with the status bar not seeming to be affected by changes in code and this solved it for me.
reference: https://stackoverflow.com/a/18184831/2962193
Set the alpha value to make it transparent.
[[UINavigationBar appearance] setAlpha:0.5f];

Visible buttons with transparent navigation bar

I've seen several apps that have completely transparent navigation bars but with visible buttons, I cant seem to find anything that wont make the button invisible as well. I'm sure they were using UINavigationController for the navbar because it had the same animations with fade and what not.
Im currently using this code in ViewDidLoad and ViewDidAppear to either hide or show the nav bar because it's not supposed to be on the first page-
[self.navigationController setNavigationBarHidden:NO animated:YES];
and this code for its transparency:
[self.navigationController.navigationBar setAlpha:0.0];
Create a subclass of UINavigationBar containing no methods except drawRect:. Put custom drawing code there if you need to, otherwise leave it empty (but implement it).
Next, set the UINavigationController's navigation bar to this subclass. Use initWithNavigationBarClass:toolBarClass: in code, or just change it in the Interface Builder if you're using storyboards/nibs (it's a subclass of your UINavigationController in the hierarchy at the side).
Finally, get a reference to your navigation bar so we can configure it using self.navigationController.navigationBar in the loadView of the contained view controller. Set the navigation bar's translucent to YES and backgroundColor to [UIColor clearColor]. Example below.
//CustomNavigationBar.h
#import <UIKit/UIKit.h>
#interface CustomNavigationBar : UINavigationBar
#end
//CustomNavigationBar.m
#import "CustomNavigationBar.h"
#implementation CustomNavigationBar
- (void)drawRect:(CGRect)rect {}
#end
//Put this in the implementation of the view controller displayed by the navigation controller
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = YES;
[self navigationController].navigationBar.backgroundColor = [UIColor clearColor];
}
Here's a screen shot of the result, mimicking Plague.
The blue border was drawn in drawRect: to show you that a UINavigationBar is there and not just a button and a label. I implemented sizeThatFits: in the subclass to make the bar taller. Both the button and label are UIView's containing the correct UI element that were placed in the bar as UIBarButtonItems. I embedded them in views first so that I could change their vertical alignment (otherwise they "stuck" to the bottom when I implemented sizeThatFits:).
self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
To make the Navigation Bar transparent, use the below code:
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
After this set the background image of the Navigation Bar the same as the view behind it using the below property:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"SAMPLE.jpg"] forBarMetrics:UIBarMetricsDefault];