Segmented Controller background grey after iOS13 - ios

I'm currently having problems with an iOS 13 segmented controller. I have this method in which I change the appearance of my segmented controller, it worked great until iOS 13 got out. now I the segmentedController has a grey background always, even if I set the background color to white, or black or whatever.
What Can I do?
- (void)modifySegmentedControl{
if (#available(iOS 13.0, *)) {
[_segmentedControl setBackgroundColor:UIColor.clearColor];
[_segmentedControl setSelectedSegmentTintColor:UIColor.clearColor];
} else {
//I had this for <iOS13, it works great
[_segmentedControl setBackgroundColor:UIColor.clearColor];
[_segmentedControl setTintColor:UIColor.clearColor];
}
[_segmentedControl setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.25 green:0.25 blue:0.25 alpha:0.6], NSForegroundColorAttributeName,
[UIFont fontWithName:#"Poppins-Medium" size:15.0], NSFontAttributeName,
nil]
forState: UIControlStateNormal];
[_segmentedControl setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.25 green:0.25 blue:0.25 alpha:1.0], NSForegroundColorAttributeName,
[UIFont fontWithName:#"Poppins-Medium" size:15.0], NSFontAttributeName,
nil]
forState: UIControlStateSelected];
self->greenBar = [[UIView alloc] init];
//This needs to be false since we are using auto layout constraints
[self->greenBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[self->greenBar setBackgroundColor:[UIColor colorWithRed:0.00 green:0.58 blue:0.27 alpha:1.0]]; //Kelley green
//
[_vistaTable addSubview:self->greenBar];
//
[self->greenBar.topAnchor constraintEqualToAnchor:_segmentedControl.bottomAnchor].active = YES;
[self->greenBar.heightAnchor constraintEqualToConstant:3].active = YES;
[self->greenBar.leftAnchor constraintEqualToAnchor:_segmentedControl.leftAnchor].active = YES;
[self->greenBar.widthAnchor constraintEqualToAnchor:_segmentedControl.widthAnchor multiplier:0.5].active = YES;
}

Try this:
if (#available(iOS 13.0, *)) {
self.segmentedControl.selectedSegmentTintColor = UIColor.redColor;
self.segmentedControl.layer.backgroundColor = UIColor.greenColor.CGColor;
}
.selectedSegmentTintColor defines the selected button color and .layer.backgroundColor the color for the whole UISegmentedControl background.
This is the result:
EDIT
It turns out this won't work for background clear or white, since on iOS 13 a sort of background image is added on the background and dividers of the segmented control:
A workaroud is create an image from color with UIGraphicsGetImageFromCurrentImageContext . The code looks like this:
- (void)viewDidLoad {
[super viewDidLoad];
if (#available(iOS 13.0, *)) {
self.segmentedControl.selectedSegmentTintColor = UIColor.redColor;
self.segmentedControl.layer.backgroundColor = UIColor.clearColor.CGColor;
[self customizeSegmentedControlWithColor: UIColor.whiteColor];
}
}
- (void)customizeSegmentedControlWithColor:(UIColor *)color {
UIImage *tintColorImage = [self imageWithColor: color];
[self.segmentedControl setBackgroundImage:[self imageWithColor:self.segmentedControl.backgroundColor ? self.segmentedControl.backgroundColor : [UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:tintColorImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[self imageWithColor:[color colorWithAlphaComponent:0.2]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:tintColorImage forState:UIControlStateSelected|UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:tintColorImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.segmentedControl.layer.borderWidth = 1;
self.segmentedControl.layer.borderColor = [color CGColor];
}
- (UIImage *)imageWithColor: (UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
Check more about it here.
This is the result for background view with color and segmented control white:
And is the result for background view white and segmented control with color:

Related

UISegmentedControll appearance no border sharp edges

I'm therribly upset. I've spent lot of time and no suitable result.
I want to redesign (AS APPEARANCE) this
to this
Or in programmatic words... from this:
+(void)configureSegmentedControls {
[[UISegmentedControl appearance] setTintColor:[COLOR_PROXY highlightColor]];
[[UISegmentedControl appearance] setBackgroundColor:[COLOR_PROXY darkBackgroundColor]];
[[UISegmentedControl appearance] setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
NSDictionary *selectedAttributes = #{NSFontAttributeName:[FONT_PROXY fontNormalOfSizeSmall],
NSForegroundColorAttributeName:[UIColor whiteColor]};
[[UISegmentedControl appearance] setTitleTextAttributes:selectedAttributes
forState:UIControlStateSelected];
NSDictionary *normalAttributes = #{NSFontAttributeName:[FONT_PROXY fontNormalOfSizeSmall],
NSForegroundColorAttributeName:[COLOR_PROXY highlightColor]};
[UISegmentedControl appearance] setTitleTextAttributes:normalAttributes
forState:UIControlStateNormal];
NSDictionary *disabledAttributes = #{NSFontAttributeName:[FONT_PROXY fontNormalOfSizeSmall],
NSForegroundColorAttributeName:[COLOR_PROXY lightGrayTextColor]};
[[UISegmentedControl appearance] setTitleTextAttributes:disabledAttributes forState:UIControlStateDisabled];
}
to this:
???
You can achieve this appearance like this:
self.segmentedControl.layer.cornerRadius = 0;
self.segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmentedControl.layer.borderWidth = 1.5;
Replace [UIColor whiteColor] with the background color of the segmented control's superview.
You can set the width of the segments (using setWidth:forSegmentAtIndex:) so you can easily make the left and right end segments larger (say 10px larger) than the others and then you can crop off 10px from either end and have square corners. You don't have to make it larger than the screen width, instead put it inside a UIView and use it to crop the ends.
On the other hand just could just make your own segmented control using a set of custom UIButtons inside a UIControl.
segmentControl.layer.borderColor = [UIColor clearColor].CGColor;
segmentControl.layer.borderWidth = 1.5;
segmentControl.tintColor = [UIColor clearColor];
if background color is white then
segmentControl.layer.borderColor = [UIColor whiteColor].CGColor;
segmentControl.layer.borderWidth = 1.5;
segmentControl.tintColor = [UIColor whiteColor];
using images this may be achieved.
[segmented setImage:nil forSegmentAtIndex:0];
[segmented setImage:nil forSegmentAtIndex:1];
[segmented setTintColor:[UIColor darkGrayColor]];
[segmented setTitle:#"1" forSegmentAtIndex:0];
[segmented setTitle:#"2" forSegmentAtIndex:1];
[segmented setDividerImage:[self imageFromColor2:[UIColor darkGrayColor] withFrame:CGRectMake(0, 0, 1, segmented.frame.size.height)]
forLeftSegmentState:(UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted)
rightSegmentState:(UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted)
barMetrics:UIBarMetricsDefault];
[segmented setBackgroundImage:[self imageFromColor2:[UIColor lightGrayColor] withFrame:CGRectMake(0, 0, segmented.bounds.size.width/2.0+1, segmented.frame.size.height)]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
to create color images:
- (UIImage *)imageFromColor2:(UIColor *)color withFrame:(CGRect)frame{
CGRect rect = frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
and as a result i had this segmented control. you can play with the colors and get what you want.
only solution (for appearance) I've found is using images only...
UIImage *activeImage = [[UIImage imageNamed:#"btn_bg_active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *inactiveImage = [[UIImage imageNamed:#"btn_bg_inactive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[UISegmentedControl appearance] setBackgroundImage:inactiveImage forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:inactiveImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:activeImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:inactiveImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

How to add a shadow effect for UINavigation bar

Hello, I want to add this kind of shadow for my NAvigationBar How can I do that.
This is how I tried to add shadow.
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage=[UIImage new];
self.navigationController.navigationBar.translucent=YES;
self.navigationController.navigationBar.topItem.titleView.tintColor=[UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes=[NSDictionary dictionaryWithObject:[UIFont fontWithName:#"HelveticaNeue" size:15.0f] forKey:NSFontAttributeName];
self.navigationController.navigationBar.topItem.title=strNavigtionTitle;
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backarrow"] style:UIBarButtonItemStylePlain target:self action:#selector(revealToggle :)];
[self.navigationController navigationBar].tintColor = [UIColor whiteColor];
[self.navigationController navigationBar].layer.shadowColor=[UIColor colorWithRed:53.0/255.0 green:108.0/255.0 blue:130.0/255.0 alpha:1.0f].CGColor;
[self.navigationController navigationBar].layer.shadowOffset=CGSizeMake(0, 20);
[self.navigationController navigationBar].layer.shadowOpacity=0.8;
[self.navigationController navigationBar].layer.shadowRadius=5.5;
But this only add a shadow for the arrow and my Apply Leave title. But I want to add a drop shadow like in this image.It should be between the NavigationBar and my main UIView How can I do this? Please help me.
Thanks
Here you'll need to import the QuartzCore framework.
self.navigationController.navigationBar.layer.borderColor = [[UIColor whiteColor] CGColor];
self.navigationController.navigationBar.layer.borderWidth = 2; //Set border you can see the shadow
self.navigationController.navigationBar.layer.shadowColor = [[UIColor blackColor] CGColor];
self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.navigationController.navigationBar.layer.shadowRadius = 3.0f;
self.navigationController.navigationBar.layer.shadowOpacity = 1.0f;
self.navigationController.navigationBar.layer.masksToBounds = NO;
Another thing
You have to
set self.layer.masksToBounds = NO;
The default value for this property is YES, which means that even though the shadow is rendered, it won't be rendered outside the bounds of the view, which means effectively that you don't see it at all.
If you're animating this view in any way, you should also add this line:
self.layer.shouldRasterize = YES;
self.navigationController.navigationBar.layer.shadowColor = [[UIColor blackColor] CGColor];
self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.navigationController.navigationBar.layer.shadowRadius = 4.0f;
self.navigationController.navigationBar.layer.shadowOpacity = 1.0f;
I could achieve that in this way. I removed adding a shadow to the Navigation bar. Instead of that I put a same size view under the navigation bar. Set the background color of it to to the navigation bar color. Then added the shadow for that view. This worked perfectly.
-(void)setupNavigationBar
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage=[UIImage new];
self.navigationController.navigationBar.translucent=YES;
self.navigationController.navigationBar.topItem.titleView.tintColor=[UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes=[NSDictionary dictionaryWithObject:[UIFont fontWithName:#"HelveticaNeue" size:15.0f] forKey:NSFontAttributeName];
self.navigationController.navigationBar.topItem.title=strNavigtionTitle;
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backarrow"] style:UIBarButtonItemStylePlain target:self action:#selector(revealToggle :)];
[self.navigationController navigationBar].tintColor = [UIColor whiteColor];
UIView *shadow=[[UIView alloc] initWithFrame:CGRectMake(0, 0, dm.screenWidth, 64)];
[shadow setBackgroundColor:[UIColor colorWithRed:62.0/255.0 green:81.0/255.0 blue:119.0/255.0 alpha:1.0]];
shadow.layer.shadowColor=[UIColor colorWithRed:51/255 green:76/255 blue:104/255 alpha:1.0].CGColor;
shadow.layer.shadowOffset=CGSizeMake(0, 15);
shadow.layer.shadowOpacity=0.12;
shadow.layer.shadowRadius=4.5;
[self.view addSubview:shadow];
}

Nav bar is translucent, but I would like transparent

I have followed the top guides on how to make my top bar transparent, but the best I can get is translucent. I am also trying to get the buttons white, not default blue. What am I missing?
#implementation FindAssetLocationMapViewController
- (void) viewWillAppear:(BOOL)animated {
self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = true;
[self.navigationController.navigationBar setTranslucent:YES];
self.navigationController.navigationBar.shadowImage = [UIImage new];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor redColor];
}
Images are below. Should I take screenshots of the FindAssetLocationMapViewController attributes as well? Just to clarify, the nav controller and nav bar attributes do not have a class associated with them.
Try this code in your app delegate, then remove the code you have:
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
// create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Place this inside application:didFinishLaunchingWithOptions:
//create background images for the navigation bar
UIImage *clear = [AppDelegate imageWithColor:[UIColor clearColor]];
//customize the appearance of UINavigationBar
[[UINavigationBar appearance] setBackgroundImage:clear forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:clear forBarMetrics:UIBarMetricsCompact];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];

iOS 6 UISegmentedControl with iOS 7 design

I'm working on an app that's supposed to work on both iOS 6 and iOS 7, and have the same flat design for both.
I'm trying to customize my UISegmentedControl to have borders, corner radius and all, but I can't figure out how to do so. I've only mange to have a flat background so far.
Does anyone have any advice to have an iOS 6 UISegmentedControl look like an iOS 7 one ?
EDIT :
I would like to have
instead of
You can use below code:
// To set colour of text
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
// Change color of selected segment
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
UIColor *newTintColor = [UIColor colorWithRed: 255/255.0 green:100/255.0 blue:100/255.0 alpha:1.0];
segmentedControl.tintColor = newTintColor;
UIColor *newSelectedTintColor = [UIColor clearColor];
[[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];
And for seting rounded corners you can use below code:
// Add rounded yellow corner to segmented controll view
[segmentedControl.layer setCornerRadius:4.0f];
[segmentedControl.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.7 blue:0.14 alpha:1.0].CGColor];
[segmentedControl.layer setBorderWidth:1.5f];
[segmentedControl.layer setShadowColor:[UIColor blackColor].CGColor];
[segmentedControl.layer setShadowOpacity:0.8];
[segmentedControl.layer setShadowRadius:3.0];
[segmentedControl.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
You can take a look at an example here or take a custom control and change its images to fit your need.
Here's an example of how you can subclass UISegmentedControl:
#implementation CustomSegmentedControl
-(id)initWithItems:(NSArray *)items
{
self = [super initWithItems:items];
if (self) {
[self setDividerImage:[UIImage imageNamed:#"segmented-control-divider-none-selected"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:[UIImage imageNamed:#"segmented-control-divider-left-selected"]
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:[UIImage imageNamed:#"segmented-control-divider-right-selected"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
// Set background images
UIImage *normalBackgroundImage = [UIImage imageNamed:#"segmented-control-normal"];
[self setBackgroundImage:normalBackgroundImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
UIImage *selectedBackgroundImage = [UIImage imageNamed:#"segmented-control-selected"];
[self setBackgroundImage:selectedBackgroundImage
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:selectedBackgroundImage
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
double dividerImageWidth = [self dividerImageForLeftSegmentState:UIControlStateHighlighted rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault].size.width;
[self setContentPositionAdjustment:UIOffsetMake(dividerImageWidth / 2, 0)
forSegmentType:UISegmentedControlSegmentLeft
barMetrics:UIBarMetricsDefault];
[self setContentPositionAdjustment:UIOffsetMake(- dividerImageWidth / 2, 0)
forSegmentType:UISegmentedControlSegmentRight
barMetrics:UIBarMetricsDefault];
return self;
}
I've finally used this submodule. It does the work :
https://github.com/pepibumur/PPiFlatSegmentedControl

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

Resources