【问题标题】:UISearchBar Minimal Style makes tint black when typingUISearchBar Minimal Style 在输入时使颜色变黑
【发布时间】:2018-06-07 20:48:08
【问题描述】:

当我在 iOS7 中将 UISearchBar 设置为 minimal style 时,当我选择它时,色调变为黑色,并且文本无法阅读,因为黑色上有黑色。

这不会产生预期的结果。选择时色调仍然是黑色...

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

【问题讨论】:

  • 我猜你是在 UISearchBar 的后台事件上调用任何网络调用??
  • @MrWaqasAhmed 什么??不...
  • 然后详细说明您的问题。
  • 你愿意清除搜索栏的背景颜色吗..???

标签: ios objective-c uisearchbar


【解决方案1】:

我遇到了同样的问题并尝试了几个小时,结论是 UISearchBar 有很多错误!尤其是在“最小”模式下。

我的解决方法是:

  • 将搜索样式设置为默认(突出)
  • 将 BackgroundImage(不是 BackgroundColor)设置为透明图像或使用 [UIColor clearColor] 创建 UIImage
  • 将 BarTintColor 设置为 [UIColor blackColor]
  • 将 TintColor 设置为 [UIColor whiteColor]

搜索栏在正常情况下看起来像最小模式, 选择时背景为白色,因此您可以看到黑色文本。

解决方法并不完美,它只是工作,希望可以提供帮助。

【讨论】:

  • 如何使用 UIColor 制作 UIImage?
  • 您可以使用CGContextSetFillColorWithColor & UIGraphicsGetImageFromCurrentImageContext 方法编写自己的代码,或者有一些开源的 UIImage 类扩展:github.com/NZN/UIImage-Helpers
【解决方案2】:

我遇到了这个错误,经过一些研究,我得到了这个有效的代码! IOS 7+

//change searchbar color
[searchBar setSearchBarStyle:UISearchBarStyleMinimal];
[searchBar setBackgroundImage:[UIImage imageWithCGImage:(__bridge CGImageRef)([UIColor clearColor])]];

【讨论】:

    【解决方案3】:
     // 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];
    }
    

    【讨论】:

    • 我的目标是让一切都变成白色,文本颜色变成黑色。你的回答不能解决这个问题。
    • 我的问题与 OP 略有不同 - 键入时文本颜色变为黑色,在深色背景上看起来很糟糕。这个答案帮助我找到了关键设置,它们是(在 Swift 中):searchBarStyle(.Minimal)、barStyle(.Black 或 .BlackTranslucent 工作)、backgroundColor(整个条的颜色)和 tintColor(文本颜色,包括打字时) .不需要其他答案的脆弱愚蠢。谢谢! BTW OP 可以通过为 backgroundColor(白色)和 tintColor(黑色)设置颜色来使用这个答案。
    【解决方案4】:
    searchField.searchBarStyle = UISearchBarStyleMinimal;
    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
    [searchField setBarTintColor:[UIColor blackColor]];
    

    将创建一个带有白色文本的黑色搜索栏。

    appearanceWhenContainedIn必须在创建searchBar并设置最小样式后调用才能真正生效。

    【讨论】:

      【解决方案5】:

      我发现如果你同时设置barstylesearchbarstyle,searchBar 会变成预期的最小样式

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

      【讨论】:

        【解决方案6】:

        您可以只编辑搜索栏属性以防止搜索栏透明:

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

        为方便起见,imageWithImage:withTintColorMethod 定义如下:

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

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题。我的搜索栏有一个白色光标,但当我开始输入时,文本是黑色的。我的搜索图标也是灰色而不是白色。我几乎是偶然发现了这个问题的解决方案。

          searchBar.setImage(UIImage(), for: .clear, state: .normal)
          

          【讨论】:

            猜你喜欢
            • 2011-11-05
            • 1970-01-01
            • 1970-01-01
            • 2015-05-30
            • 2016-11-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多