In my Project i am using segment Controller .. they have four segment in my segment Controller ..My question is I want this background color and font color and State selected color and separator color[White when select the segment]
Like this Image
.
But My screen is
My code Is
- (void)viewDidLoad {
[self changeColor];
}
- (void)changeColor{
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithRed:83.0f/255.0f green:198.0f/255.0f blue:255.0f/255.0f alpha:1.0]} forState:UIControlStateSelected];
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithRed:197.0f/255.0f green:197.0f/255.0f blue:197.0f/255.0f alpha:1.0]} forState:UIControlStateNormal];
[mailboxsegment setTintColor:[UIColor colorWithRed:202.0f/255.0f green:202.0f/255.0f blue:202.0f/255.0f alpha:1.0]];
UIFont *font = [UIFont boldSystemFontOfSize:09.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[mailboxsegment setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
my code i will try to change background color and change font size
please try this one
- (void)segmentAction:(UISegmentedControl *)segment
{
UIColor *selectedColor = [UIColor whiteColor];
UIColor *deselectedColor = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
for (UIControl *subview in [segment subviews])
{
if ([subview isSelected])
[subview setTintColor:selectedColor];
else
[subview setTintColor:deselectedColor];
}
}
You can create your custom segmented control. But its little complicated to create custom segmented control as you will have to provide images for selected and deselected states, 1px image for separator etc.
Instead of this i will suggest you to use four different buttons and apply logic to select only one button at a time.
To get effect of separator color, place these four buttons inside a stack view or a normal UIView and set background color of that container view to the color which you want to set for separator.
Related
I am using UISegmentedControl in my app and i could able to toggle the text color when it changes the state. But by default selected segment contains a different tint color like below image
I do not want to have different tint color when it is in selected state. I just wish to differentiate the selected segment only by the text color like below image.
I know this is a silly question but none of the other similar questions provide me a proper answer.
Can you anyone please guide me to achieve this?
Thanks in anticipation!
I would strongly suggest not doing this.
You realise that this just makes it harder to use right? For a colour blind person (about 1 in 11 men are colourblind) your version is pretty much impossible to use. For people with poor sight it's very hard to use. The best test for usability (and advice from Apple engineers) is to turn it into greyscale images and see if you can still use it.
For example...
Section tint
becomes...
Using a contrasting colour text on the selected index would improve this.
Your colour scheme
becomes...
This is very hard to determine which section is selected.
This is enough.
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:17], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[_segment setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
If you want to change the color when the segment change
- (IBAction)horseSegmentChanged:(UISegmentedControl*)sender {
if (sender.selectedSegmentIndex == 0) {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:17], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[_segment setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
} else if (sender.selectedSegmentIndex == 1) { // selected shared blogs
}
}
Find the below customsegment control
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface CustomSegmentControl : UISegmentedControl
#end
#import "CustomSegmentControl.h"
#interface CustomSegmentControl ()
- (UIImage *)imageWithColor:(UIColor *)color;
#end
#implementation CustomSegmentControl
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[self setBackgroundImage:[[UIImage imageNamed:#"img.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:[[UIImage imageNamed:#"img.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
}
#end
This will solve your issue
I agree with Fogmeister that this is probably not a very good idea in terms of usability. That said, UISegmentedControl allows you to set a background image for a specific state. You could set an image with the same color for each of the UISegmentedControl states, and that would give you the effect you desire.
To programmatically create an image from UIColor, see here.
And some sample code for a segmented control with an always white background (considering an UIImage category as the one referenced above):
[self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
UPDATE ANSWERED. BY ME.
Currently having problems with the text color change of my UISegmentedControl; it needs to change on first load with UIControlStateSelected. Code works, but only conditionally. It works when you visit the page with the segmented control on the navigation bar, hit the back button, and then visit the page again. I'm assuming there's a problem with inheritance here. Let me explain..
The location of the the segmented control lies on top of my navigation bar.
Inheritance of the ViewController which contains the SegmentedControl:
TabBarViewController(managed with AppDelegate)-->navigation Controller-->ViewController(where 'inviteSegBar' lies)
Here's the code within AppDelegate.m:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithHexString:#"#669900"]];//this one sets it green.
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
And here's the viewDidLoad: code for the VC which contains 'inviteSegBar', the UISegmentedControl in question:
- (void)viewDidLoad
{
[super viewDidLoad];
//CUSTOM APPEARANCE <below>
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"#669900"];
inviteSegBar.tintColor = [UIColor colorWithHexString:#"#333333"];
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
}
Like I said the last line works, but only when you re-visit the page. Why is this happening?
PS This is the same issue guys, I had already tried this code before any of the answers were listed.
ANSWER FOUND: Simply move
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
to your AppDelegate.m file
Use
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
update
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
This code allows you to set some text attributes for label in segmented control:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];
More allowed attributes in Apple documentation: link
This may help you:
UIAppearance proxy to set title text attributes but preserve tintColor for borders.
[[UISegmentedControl appearance] setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor redColor]
} forState:UIControlStateNormal];
For change UISegmentedControl appearance insert for example into viewDidLoad function this code:
// color selected text ---> red
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateSelected];
// color disabled text ---> blue
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];
// color tint segmented control ---> black
[[UISegmentedControl appearance] setTintColor:[UIColor greenColor]];
I'm trying to change the Navbar tint at runtime, and I'm able to change the navigation bar tint, but then the left and right buttons (which were system blue on iOS 7) changed, so I'm trying to force them back to the system blue color. this is the code I am using, called from viewDidLoad: I've tried both NSForegroundColorAttributeName and UITextAttributeTextColor, but I keep getting white or black text, depending on the color of the navigation bar.
How can I get the buttons to show blue text?
NSDictionary *btnAttr = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:14], UITextAttributeFont,
[UIColor blueColor], NSForegroundColorAttributeName,
nil];
for(UIView *v in [theView.view allSubViews])
{
if([v isKindOfClass:[UINavigationItem class]])
{
UINavigationItem *btn = (UINavigationItem *)v;
[btn.leftBarButtonItem setTitleTextAttributes:btnAttr forState:UIControlStateNormal];
[btn.rightBarButtonItem setTitleTextAttributes:btnAttr forState:UIControlStateNormal];
}
}
In iOS7 you if you need to change the navigationBar buttons color, you need to set tintColor:
navigationController.navigationBar.tintColor = [UIColor blueColor];
For better understanding check the image below:
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
In my iOS5 iPhone application, i'm setting the tint color of search bar using following code:
searchBar.tintColor = UIColorMake(#"#EFEFEF");
RGB value of #efefef is (239,239,239)
Its working fine. But when cancel button appears the text "Cancel" is not visible. Can I customize only the cancel button with transparent black and white text on that?
is it possible to customize?
You can customize the Cancel button on iOS 5 by using the appearance proxy.
You need to change appearance of UIBarButtonItem when contained in UISearchBar.
For example to change the title font of the Cancel button you can use:
NSDictionary *attributes =
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont systemFontOfSize:12], UITextAttributeFont,
nil];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
setTitleTextAttributes:attributes forState:UIControlStateNormal];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
setTitleTextAttributes:attributes forState:UIControlStateHighlighted];
You could search for UISearchBar subViews and locate the cancel button, it is dangerous to do so, since the button could change
For example you could add this in your viewWillAppear
- (void) viewWillAppear:(BOOL)animated
{
//show the cancel button in your search bar
searchBar.showsCancelButton = YES;
//Iterate the searchbar sub views
for (UIView *subView in searchBar.subviews) {
//Find the button
if([subView isKindOfClass:[UIButton class]])
{
//Change its properties
UIButton *cancelButton = (UIButton *)[sb.subviews lastObject];
cancelButton.titleLabel.text = #"Changed";
}
}
}
As i said before this could change, its a hack to do so, you better stick with the original, or create your own search bar.
Since iOS5 you can edit the Navigationbar, Toolbar, Tabbar and some more with this code...
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor darkGrayColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
I havenĀ“t tested it with a searchbar, but it should work similar.
This method works in IOS7
for (UIView *view in searchBar.subviews)
{
for (id subview in view.subviews)
{
if ( [subview isKindOfClass:[UIButton class]] )
{
// customize cancel button
UIButton* cancelBtn = (UIButton*)subview;
[cancelBtn setEnabled:YES];
break;
}
}
}
Check this https://stackoverflow.com/a/18150826/1767686