i'm trying to change the color of a tabbar in my app, i use this line of code to change the color of it:
[[UITabBar appearance] setBarTintColor:[UIColor greenColor]];
I want to add a translucent iOS 7 effect but with a green color. I've changed translucent property but i don't see any result.
Change UITabBarController's alpha:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
[tabBarController.tabBar setBarTintColor:[UIColor greenColor]];
[tabBarController.tabBar setAlpha:0.2];
}
or with appearance in the same application: didFinishLaunchingWithOptions:
[[UITabBar appearance] setBarTintColor:[UIColor greenColor]];
[[UITabBar appearance] setAlpha:0.2];
You can either set the color on the storyboard by selecting the root: Tab Bar View Controller, select the tab bar, and adjust the Background (or tint) color in the attributes inspector, or you can adjust the code with the barTintColor:
// Adjust the Color of the Tab Bar itself
self.tabBar.barTintColor = [UIColor redColor];
// Adjust the Color of the selected Icon in the Tab Bar
self.tabBar.tintColor = [Single single].singleThemeColorTint;
If you need to adjust the ALPHA too, I would use:
UIColor *charcoal = [UIColor colorWithRed:66/255.0
green:79/255.0
blue:91/255.0
alpha:1];
// For Tab Bar
self.tabBar.barTintColor = charcoal;
// For selected Item Highlight
self.tabBar.tintColor = charcoal;
I created a View Controller File for the Tab Bar Story Board, and ran this code in ViewDidLoad{ }
Related
I am having two controller with different navigation bar color. let say I want to set first controller with navigation bar black color and TitleTextAttributes color whiteColor and after navigate to second controller it change navigation bar white color and TitleTextAttributes black color. navigation bar color change but title text attribute is not changing. Please suggest.
i write this code and used in viewWillAppear method.
Inside navigation bar category
typedef NS_ENUM( NSUInteger, UINavigationBarColor) {
White,
Black
};
+(void)setNavigationColor:(UINavigationBarColor)color{
if (color == White) {
[[self appearance] setTintColor:[UIColor whiteColor]];
NSDictionary *attributes = #{NSForegroundColorAttributeName:[UIColor blackColor]};
[[self appearance] setTitleTextAttributes:attributes];
} else {
[[self appearance] setTintColor:[UIColor blackColor]];
NSDictionary *attributes = #{NSForegroundColorAttributeName:[UIColor whiteColor]};
[[self appearance] setTitleTextAttributes:attributes];
}
}
Your code is setting the tintColor and titleTextAttributes on UINavgiationBar appearance. That only affects newly created navigation bars. It does not change any existing navigation bars.
You need to make setNavigationColor an instance method and change [self appearance] to just self.
I want to change background color of status bar and that background color should be same as navigation bar background color i.e dark gray color. My problem is after giving the same color for both status bar and navigation bar using my below code, i am getting the different color. I have already set UIViewControllerBasedStatusBarAppearance to NO. Please suggest me where i am doing wrong, i have attached below image for your reference.
here is my code of App delegate - didFinishLaunchingWithOptions:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//For changing status bar background color
self.window.backgroundColor = [UIColor darkGrayColor];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
//For changing navigation bar background color
[[UINavigationBar appearance] setBackgroundColor:[UIColor darkGrayColor]];
[[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]];
The reason they have different colors is because UIStatusBarStyleLightContent isn't the same color as [UIColor darkGrayColor].
If it's fine to adjust navigation bar color after how the status bar looks like, then you can set the status bar color:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Find out the RGB values of the status bar. (For example, I use DigitalColor Meter, it says it has R:28, G: 28, B: 28).
Then you can do something like:
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:28/255.0 green:28/255.0 blue:28/255.0 alpha:1.0f]];
You might need to set translucency to NO.
If you only want it on certain screens, you can add this to the viewDidLoad method:
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:28/255.0 green:28/255.0 blue:28/255.0 alpha:1.0f]];
First Change the statusBar style in your VC's viewDidLoad method
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
And then change the BarTintColor to your desired color in your VC's viewDidLoad method
[self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
In your AppDelegate's 'didFinishLaunchingWithOptions:' add following and it should work.
[[UINavigationBar appearance] setBarTintColor:[UIColor darkGrayColor]];
Earlier I used a navigationcontroller with this code :
self.navigationItem.title = #"News";
Now I have the problem that I no longer needed the navigationcontroller as I am using a page controller for navigation. Now I added a navigationbar however it doesn't change the title with this code anymore.
Also how can I change the background color?
IN iOS 7 use write following code in didFinishLaunchingWithOptions
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)])
{
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed: 4.0/255.0 green:173.0/255.0 blue:214.0/255.0 alpha:1.0f ]]; //// change background color of navigationBar
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; /// set backButton color of navigation bar
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}]; // set title color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; /// set all barButton item color
self.navController.navigationBar.translucent = NO;// set translucent NO
}
Use as per your requirement.
So there is some magic happening when you use a navigation controller that you don't see and now that you've made the switch to justing using a navigation bar you lose that magic.
Navigation bars are responsible for managing and presenting UNavigationItems, and as such hold an array of them. A NavigationItem holds things like left and right buttons, and the title or titleView. When your navigation controller pushes a new controller, it creates a brand new UINavigation item then links it to your new view controller, and then pushes that onto navigation bars stack of nav items. That's why from within your view controller you set the title with self.navigationItem.title instead of referencing the navbar.
Basically you have to manage the bar and nav items yourself now. This should about do it for you:
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, yOffset, CGRectGetHeight(size), height)];
_navItem = [[UINavigationItem alloc] initWithTitle:#"My navbar title"];
[_navBar setItems:#[_navItem]];
[self.view addSubview:_navBar];
Of course you'll have to manage the size a bit differently as my example comes from an app where it's being used in landscape mode.
When i am trying to change the navigation bar tintcolor in split view the masterviewcontroller's navigationbarcolor chenged but detailviewcontroller navigation bar color not chenged. here is my code :
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
[self.navigationController popViewControllerAnimated:YES];
i want to change the navigation bar color throughout the application in iOS7 by clicking on a button can any one help me?
You can simply do it by:
yourDetailViewController.navigationBar.barTintColor = [UIColor blueColor];
I'd like to change the background image (or color) of the navigation bar, but for some reason it's not working.
I've searched around, and found that on iOS 5.0+ (I'm running 6.1) this should work:
[self.navigationController.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
Now none of those work. Also tried with:
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
Now I'm thinking that I'm assigning it to the wrong object, but this works:
UIImageView *titleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"scr1_title"]];
self.navigationItem.titleView = titleImage;
Use this code in your view controller to change the navigation bar background image.
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
UIImage * navBarImage = [UIImage imageNamed:#"image.png"] ;
[[UINavigationBar appearance]setBackgroundImage: navBarImage forBarMetrics:UIBarMetricsDefault];
}
You can set the tint color on the navigation bar of the navigation controller. Make sure you select the correct on as shown in the screenshot below.
Select your storyboard in the file browser.
Select the navigation controller in the storyboard.
In the Document Outline (second column) select the navigation bar of the navigation controller.
In the Utilities view (column on the right) adjust the tint color.
Click on the root view controller to the right on the navigation controller. It will pick up the new color after you click on it.
To set the image..
Write this code
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationBar"] forBarMetrics:UIBarMetricsDefault];
inside
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}