iOS - Customizing Cancel button of UISearchBar - ios

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

Related

TextField text Color change in searchbar

Please check my below code, I use one navigationbar which is consist a searchbar and searchbar have a textfield, now i want to change a text color of textfield and i did a below code but it is not working so please help to correct it.
[[UISearchBar appearanceWhenContainedInInstancesOfClasses:#[[UINavigationBar class]]]setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]]setTextColor:[UIColor whiteColor]];
If you are using a native UISearchbar control then you may change text color with the code below
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor]}];
Or you can do it for a specific searchBar via Identity Inspector:
Try this,
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
if([view isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField*)view;
UIImageView *imgView = (UIImageView*)textField.leftView;
imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imgView.tintColor = [UIColor whiteColor];
txt.backgroundColor = [UIColor blackColor];
txt.textColor = [UIColor RedColor];
UIButton *btnClear = (UIButton*)[textField valueForKey:#"clearButton"];
[btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
btnClear.tintColor = [UIColor whiteColor];
}
}

How to Change Segment Separator Color?

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.

Active UISearchBar makes status bar background black

I have a UISearchBar at the top of UITableView which works great. I've been trying to customise its appearance and there is an issue when the search bar is active - it turns the status bar background colour to black. I tried the suggestions in IOS7 Status bar change to black after search is active but it didn't work for me.
App is iOS 7-only. UISearchBar is to to Minimal Search Style and Default Bar Style, translucent and default barTintColor. I've customised its appearance in my AppDelegate as follows:
[[UISearchBar appearance] setBarTintColor:AAColorInputBorder];
[[UISearchBar appearance] setBackgroundColor:AAColorInputBorder];
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIFont fontWithName:#"Avenir" size:16], NSFontAttributeName,
nil] forState:UIControlStateNormal];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"Avenir" size:14]];
Does anyone have any idea on how to change the status bar background color? I want it to match the colour of the UISearchBar.
Thanks!
This is a little hack I use to extend the UISearchBar color
on viewDidLoad()
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
method:
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view {
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}

Unable to change UISearchBar cancel button title color after changing it's text.

I'm using this code to change UISearchBar cancel button title:
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIView* view=_otsinguRiba.subviews[0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
if (cancelButton) {
[cancelButton setTitle:#"Test") forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
}
While changing the text works fine, changing the color doesn't. It stays black.
I found the answer to this on SO a while back, but I don't recall where. Here's the code that I'm using to set the color of the text.
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],NSForegroundColorAttributeName,
//[UIColor whiteColor],UITextAttributeTextShadowColor,
//[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
You can take advantage of the iOS Runtime Property _cancelButton to achieve this.
UIButton *cancelButton = [self.searchDisplayController.searchBar valueForKey:#"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];

UISegmentedControl only changes text color when revisiting ViewController

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]];

Resources