Change text color of search bar ios - ios

Is it possible to change the text color of the search bar? I don't seem to have access to the UISearchBarTextField class...

firstly, you find the subview in UISearchBar, then find the UITextField in sub View then change color
Try this code:-
for(UIView *subView in searchBar.subviews){
if([subView isKindOfClass:UITextField.class]){
[(UITextField*)subView setTextColor:[UIColor blueColor]];
}
}
for Ios 5 +
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

As of iOS 5, the right way to do this is using the appearance protocol.
For example:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

You can set the attributes like so, call this in your controller.
[[UITextField appearanceWhenContainedIn:[self class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
*note that this will change all UITextFields in your controller

The original UISearchBar hierarchy has changed since the original post and the UITextField is no longer a direct subview. The below code makes no assumptions about the UISearchBar hierarchy.
This is also useful when you don't want to change the search bar's text color throughout the entire application (i.e. using appearanceWhenContainedIn).
/**
* A recursive method which sets all UITextField text color within a view.
* Makes no assumptions about the original view's hierarchy.
*/
+(void) setAllTextFieldsWithin:(UIView*)view toColor:(UIColor*)color
{
for(UIView *subView in view.subviews)
{
if([subView isKindOfClass:UITextField.class])
{
[(UITextField*)subView setTextColor:color];
}
else
{
[self setAllTextFieldsWithin:subView toColor:color];
}
}
}
Usage:
[MyClass setAllTextFieldsWithin:self.mySearchBar toColor:[UIColor blueColor]];

Related

Cant change uitextfield background color inside uisearchbar iOS8.4

Im trying to change the background color of the UITextField inside of an UISearchBar. To do this, I have tried the following:
-This simple one-liner of code:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor redColor]];
This for loop:
for (UIView *subView in _searchBar.subviews) {
for(id field in subView.subviews){
if ([field isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)field;
[textField setBackgroundColor:[UIColor grayColor]];
}
}
}
and this code:
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];
I'd like to avoid changing the background image of the UISearchBar, so what's the new solution for iOS8? I've found no working answer on stack.

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

Change text color and border color of a UISearchBar

I have an UISearchBar, with search style "minimal", and I want to change the text color and to have a white border with 1px width. I'm trying with tintcolor but I don't get nothing
Thank you.
To change the text color of the UISearchBar, use this snippet
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor yourColor]];
For the white border, try this out
for (id object in [[[yourSearchBar subviews] objectAtIndex:0] subviews])
{
if ([object isKindOfClass:[UITextField class]])
{
UITextField *textFieldObject = (UITextField *)object;
textFieldObject.textColor = [UIColor redColor];
textFieldObject.layer.borderColor = [[UIColor whiteColor] CGColor];
textFieldObject.layer.borderWidth = 1.0;
break;
}
}
Hope this helps!
Edit
You can add the first line for the search bar color anywhere in your app. Also, I have added one line for the textColor in the loop, check that out.

How to change properties of UITextField inside UISearchBar

Basically I am accessing this URL
UITextField * textField= self.searchBar.textField;
Let's just say the function .textField does get the textField. I have verified this several times.
textField.adjustsFontSizeToFitWidth =true;
textField.minimumFontSize =10;
textField.enablesReturnKeyAutomatically =NO;
I followed with the code above.
Still the UITextField inside self.searchBar doesn't change font size when text is too long.
I wonder why.
Try to get your text field like this....
UITextField *textField = [searchbar valueForKey:#"_searchField"];
And then customize it according to you.
textField.adjustsFontSizeToFitWidth =true;
textField.minimumFontSize =10;
textField.enablesReturnKeyAutomatically =NO;
You should try using UIAppearance instance method when you want to change some class instance which is contained in other class instance. (Looking for the view hierarchy is the work around).
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
Here it is well explained in documentation by Apple.
Try this code..This way you can change and manage properties of UITextField inside UISearchBar
for (UIView *searchBarSubview in [searchbar subviews])
{
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
#try
{
[(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"jots" size:10]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] adjustsFontSizeToFitWidth];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] enablesReturnKeyAutomatically];
}
#catch (NSException * e)
{
// ignore exception
}
}
}
Hope it helps you..

Changing UISearchBar search field background

I have a UISearchBar contained within a UIView and the UIView has been assigned to a UITableView's tableHeaderView. I'm trying to style the UISearchBar to look like this:
Out of the gate, this is what it looks like:
There are loads of SO questions and answers on how to style UISearchBar, its search field, etc., some with particular attention to backgrounds. Many of them involve traversing the UISearchBar's subviews, finding a UISearchBarBackground (or _UISearchBarSearchFieldBackgroundView) object, and setting its background color directly. I'm trying to find a supported API for doing this, however...
The setSearchFieldBackgroundImage:forState: method seems to have great potential, but this is what I get when I use a 1px white (or transparent) image using UIControlStateNormal:
Any ideas as to how to get this to work? I'd appreciate someone pointing me to a SO post that answers this, but I really do think I've read them all. Thanks a ton.
you can use the appearanceWhenContainedIn to target elements without traversing the view hierarchy. Something like:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor lightGrayColor]];
UPDATE:
As appearanceWhenContainedIn is now deprecated, use
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar self]]] <your setter here>];
I think the problem here is that when you use setSearchFieldBackgroundImage:forState:, the height of your image actually determines the height of the search field. The standard search field (at least as of iOS 8) is 28 points tall, so an image that height should give the effect you're after.
(Somewhat confusingly, this also means that a nine-part stretchable image generally won't look right—so if you were going for a look that wasn't just white on white, you need to use a three-part stretchable image that only stretches horizontally.)
Above iOS 5.0
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:#"YourBackground.png"]forState:UIControlStateNormal];
Please try following code
[[UISearchBar appearance]setBackgroundImage:[UIImage imageNamed:#“searchbar_bg.png"]];
for (UIView *searchbuttons in searchBar.subviews) {
if ([searchbuttons isKindOfClass:[UIButton class]]) {
UIButton *cancelbutton = (UIButton *)searchbuttons;
[cancelbutton setBackgroundImage:[UIImage imageNamed:#“canelBtn.png"] forState:UIControlStateNormal];
}
}
UITextField *searchField = [searchBar valueForKey:#"_searchField"];
searchField.background = [UIImage imageNamed:#"bg.png"];
You can use either the barTintColor or the backgroundImage property of the UISearchBar to directly accomplish what you are trying to do.
I reproduced your problem, and it seems to be solved by:
self.searchDisplayController.searchBar.barTintColor = [UIColor whiteColor];
or
self.searchDisplayController.searchBar.backgroundImage = [self imageWithColor:[UIColor whiteColor]];
P.S. If you are using the backgroundImage solution, you'll need to make the method imageWithColor as follows
- (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 *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Tell me if it works!
iO9 Perfect works.
- (void)viewDidLoad
{
[super viewDidLoad];
[[self searchSubviewsForTextFieldIn:self.searchBar] setBackgroundColor: [UIColor redColor]];
}
- (UITextField*)searchSubviewsForTextFieldIn:(UIView*)view
{
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
UITextField *searchedTextField;
for (UIView *subview in view.subviews) {
searchedTextField = [self searchSubviewsForTextFieldIn:subview];
if (searchedTextField) {
break;
}
}
return searchedTextField;
}
#joneswah's answer is depricated in iOS 9.
For iOS 9 put this in your AppDelegate.m. I also added workaround for keyboard opening lag, when keyboard is initiated for the first time.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Remove lag on oppening the keyboard for the first time when clicked on any UITextField
UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
//searchBar background color change
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setBackgroundColor:[UIColor greenColor]];//background color
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setTextColor:[UIColor blackColor];//text color
return YES;
}
As others suggested, you need to traverse through your searchbar's subviews and finding the UITextField instance. BUT, you need to set its layer.backgroundColor, as setting the backgroundColor property has no effect. I'll give an example:
for view in searchBar.subviews {
for subview in view.subviews {
if let searchField = subview as? UITextField {
searchField.layer.backgroundColor = UIColor.whiteColor().CGColor
}
}
}
This will give you what you need.
My answer stably works on ios 5 and above, including this 10. Without hacks and KVO, by Apple Way
The following code will achieve the result.
_sbAddress.barTintColor = [UIColor whiteColor];
for (UIView *subView in _sbAddress.subviews) {
for(id field in subView.subviews){
if ([field isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)field;
[textField setPlaceholder:#"Search"];
}
}
}
OR
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setPlaceholder:#"Search"];

Resources