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];
}
}
}
Related
I'm trying to change the color of the search bar orange and the bar tint color to white when a contact get selected.
No matter what I've been trying, nothings get changed and it's stuck like this (back button isn't white).
in AppDelegate:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil] setBarTintColor:OrangeOfficialColor];
[[UINavigationBar appearance] setBarTintColor:OrangeOfficialColor];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
When popping the view:
ABPeoplePickerNavigationController *contactsPicker = [[ABPeoplePickerNavigationController alloc] init];
contactsPicker.peoplePickerDelegate = self;
contactsPicker.displayedProperties = #[[NSNumber numberWithInt:kABPersonPhoneProperty]];
[contactsPicker.searchDisplayController.searchBar setBarTintColor:OrangeOfficialColor];
[contactsPicker.searchDisplayController.searchBar setTintColor:[UIColor whiteColor]];
[self presentViewController:contactsPicker animated:YES completion:nil];
View is OK:
View isn't OK:
View isn't OK:
I had the same issue once I started using Xcode 6 (iOS 8 SDK) and iOS 7 apps. This was my workaround fix:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue" size:16.0f], NSForegroundColorAttributeName : [UIColor blueColor]} forState:UIControlStateNormal];
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?)
I want my UISearchbar should look like this.
i've tried this:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"Helvetica Neue" size:20]];
[self.seachBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"background"] forState:UIControlStateNormal];
[self.seachBar setImage:[UIImage imageNamed:#"SearchIcon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
[self.seachBar setBackgroundImage:[UIImage imageNamed:#"background"]];
[self.seachBar setText:#"Search"];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
Please guide or any tutorial.
Thanks
Try this code
for (UIView *searchBarSubview in [searchBar subviews])
{
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:#"background"]forState:UIControlStateNormal];
[(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleNone];
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:#"search"]];
[[UISearchBar appearance] setTintColor:[UIColor clearColor]];
}
}
Hope, your problem solved...
refer below link it more helpful you don't waste your valuable time
Right align magnifying glass icon in UISearchBar
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
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]];