Вопрос

When I set my UISearchBar to minimal style in iOS7, when I select it the tint becomes black, and the text is impossible to read because there's black on black.

This does not produce the desired result. The tint is still black when selected...

if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
    // set bar style
    _sendToSearchBar.barStyle = UIBarStyleDefault;
    // set bar transparancy
    _sendToSearchBar.translucent = NO;
    // set bar color
    _sendToSearchBar.barTintColor = [UIColor whiteColor];
    // set bar button color
    _sendToSearchBar.tintColor = [UIColor whiteColor];
    // set bar background color
    _sendToSearchBar.backgroundColor = [UIColor whiteColor];
}

enter image description here

Это было полезно?

Решение

I had same problem and tried for few hours, the conclusion is UISearchBar are quite buggy ! Especially in "minimal" mode.

My workaround is :

  • Set the search style to Default (Prominent)
  • Set the BackgroundImage (Not BackgroundColor) to a transparent image or create an UIImage using [UIColor clearColor]
  • Set the BarTintColor to [UIColor blackColor]
  • Set the TintColor to [UIColor whiteColor]

The search bar looks like minimal mode in normal, and the background will be white when selected, so you can see the black color text.

The workaround isn't perfect, it just work, hope can help.

Другие советы

i had this bug and after some research i get this code that work! ios 7+

//change searchbar color
[searchBar setSearchBarStyle:UISearchBarStyleMinimal];
[searchBar setBackgroundImage:[UIImage imageWithCGImage:(__bridge CGImageRef)([UIColor clearColor])]];
 // iOS7 and after
if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
    // set bar style
    bar.barStyle = UIBarStyleBlack;
    // set bar transparancy
    bar.translucent = NO;
    // set bar color
    bar.barTintColor = [UIColor blackColor];
    // set bar button color
    bar.tintColor = [UIColor blackColor];
    // set bar background color
    bar.backgroundColor = [UIColor blackColor];
}
searchField.searchBarStyle = UISearchBarStyleMinimal;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
[searchField setBarTintColor:[UIColor blackColor]];

Will create a black tinted search bar with white text.

The appearanceWhenContainedIn must be called after creating the searchBar and setting the minimal style to actually take effect.

I Discover that if you set both barstyle and searchbarstyle, searchBar turn to expected minimal style

self.itemsSearchController.searchBar.barStyle = UIBarStyleDefault;
self.itemsSearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

You can just edit search bar properties to prevent making search bar transparent:

self.iSearchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
self.iSearchController.searchBar.backgroundImage = [UIImage imageWithImage: [UIImage imageNamed:@"whiteBackgroundImage.png"] withTintColor:[UIColor blackColor]]; // or you can just create any random plain image with this color and use it with imageNamed: method.
self.iSearchController.searchBar.barTintColor = [UIColor blackColor];

For your convenience imageWithImage:withTintColorMethod is defined below:

@implementation UIImage (Category)
+ (UIImage *) imageWithImage: (UIImage*) image withTintColor: (UIColor*) color {
    UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIGraphicsBeginImageContextWithOptions(newImage.size, NO, newImage.scale);
    [color set];
    [newImage drawInRect:CGRectMake(0, 0, newImage.size.width, newImage.size.height)];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

I had the same problem. My search bar had a white cursor but when I started typing the text was black. Also my search icon was gray instead of white. I found the solution to this almost by accident.

searchBar.setImage(UIImage(), for: .clear, state: .normal)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top