UISearchBar text color when used in UISearchDisplayController - ios

i'm trying to change a UISearchbar text color in iOS7 app. (i want the text to be white).
the search bar is attached to a searchDisplay controller.
i have tried many options, including this one:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], [FVRNavigationBar class], nil] setTextColor:[UIColor whiteColor]];
also tried setTintColor:, and setBarTintColor:. Nothing works.
Note, this solutions does work when the search bar is not attached to a displayController.
FYI, not that i think it matters, but i use a background image for my search bar.
in addition, the searchbar is placed in my navigationBar.
here is my code:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], [FVRNavigationBar class], nil] setTextColor:[UIColor whiteColor]];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 52)];
[_searchBar setSearchFieldBackgroundImage:[IMAGE(#"searchBarBg") resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)] forState:UIControlStateNormal];
_displayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_displayController.delegate = self;
_displayController.displaysSearchBarInNavigationBar = YES;
I'd appreciate anyones help. thanks!

Related

UIsearchbar show a line below it which never goes

I have a UISearchBar whose bartintcolor i have changed and have cancel enabled. This is added as a subview of a UIVew.
whenever make search bar visible, it shows a black line below it which never goes away.
Am i missing something about UISearchbar appearence or its background view.
dummyview=[[UIView alloc]initWithFrame:CGRectMake(10, 30,self.headerView.frame.size.width-20, 30)];
[dummyview setBackgroundColor:[UIColor redColor]];
[dummyview setClipsToBounds:TRUE];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, dummyview.frame.size.width, dummyview.frame.size.height)];
self.searchBar.delegate = (id)self;
[self.searchBar setPlaceholder:#"Search"];
self.searchBar.showsCancelButton = YES;
_searchBar.barTintColor = [Utilities colorWithHexString:#"3a7ebc"];
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
_searchBar.delegate=self;
[dummyview addSubview:_searchBar];
it gives me like this
if i change background to native one, i get displaced content.
// _searchBar.barTintColor = [Utilities colorWithHexString:#"3a7ebc"];
I just dont want the black line below the searchbar.
Updated:
using
_searchBar.searchBarStyle=UISearchBarStyleMinimal;
_searchBar.barStyle=UIBarStyleDefault;
result in this
So
finally everything done
_searchBar.searchBarStyle=UISearchBarStyleMinimal;
_searchBar.barStyle=UIBarStyleDefault;
UITextField *searchField=[_searchBar valueForKey:#"_searchField"];
if (searchField) {
[searchField setTextColor:[UIColor whiteColor]];
}
Put my code in viewdidload or viewwillappear method.
searchbar.barTintColor=[UIColor samebgcolor];
searchbar.layer.borderWidth = 1;
searchbar.layer.borderColor = [[UIColor samebgcolor] CGColor];
I would also consider, "I hope this will help you", as this is the strongest, and hopes that the thing will most definitely, certainly help.
If you like my Answer so accept and upvote my answer

Cannot change searchbar text color, but I am able to change background color

I have a searchbar embedded in a navigationbar. I'm able to change background color, but for the life of me cannot change text color. I have the following code in my viewdidload. What is missing to get the text to change?
if(!itemSearchBar)
{
itemSearchBar = [[UISearchBar alloc] init];
[itemSearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
itemSearchBar.placeholder = #"What are you looking for?";
itemSearchBar.delegate = self;
UITextField *txfSearchField = [itemSearchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor colorWithRed:130/255.0 green:58/255.0 blue:23/255.0 alpha:1.0];
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:itemSearchBar
contentsController:self ];
[searchCon setActive:NO animated:YES];
self.searchDisplayController = searchCon;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.displaysSearchBarInNavigationBar=YES;
I've read that the following should work but doesn't:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
I've also tried:
itemSearchBar.tintColor = [UIColor redColor];
txfSearchField.textColor = [UIColor redColor];
Any suggestions???
I think you are looking at the color of placeholder text and not typing anything in the textfield. Otherwise, your code seems correct. The method
txfSearchField.textColor = [UIColor redColor]
must work. Type anything in the field and you will see red color.

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 the position of backgroundImage when clicked on searchBar

I have a background image in my search bar:
self.searchBar.scopeButtonTitles=nil;
self.searchBar.showsScopeBar=NO;
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:#"search-icon-1"]forState:UIControlStateNormal];
[self.searchBar.layer setBorderWidth:1];
[self.searchBar.layer setBorderColor:RGB(200,200,200).CGColor];
self.searchBar.layer.cornerRadius=12;
[self.searchBar sizeThatFits:CGSizeMake(216, 56)];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setFont:opensans2];
By default, when I clicked on my search bar, my background image is moved to the left.
How can I replace it on the center ? ( -> How can i access to the variable background offset horizontal, which is present in the storyboard, programmatically?)

Change font for a programmatically created UISearchBar

I have found that the code in my AppDelegate:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
setFont:[UIFont systemFontOfSize:14]];
works to change the font of a UISearchBar created from IB. No problems there.
However, if I create the UISearchBar in code as such:
UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
bar.placeholder = #"test";
[self.view addSubview:bar];
Then the above UITextField appearance code has no effect. Is there a way to change the font of a programmatically created UISearchBar?
Thank you!
Try this,It's Working Fine for iOS 5.0 and up: (iOS 7 also)
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"League Gothic" size:20]];
You can try this
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:#"Helvetica" size:25]];
for more reference check this
You can use this
UITextField *textField = [self.searchBar valueForKey: #"_searchField"];
[textField setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0]];
Hope this will help.

Resources