【问题标题】:Unable to set correct background color to UISearchTextField无法为 UISearchTextField 设置正确的背景颜色
【发布时间】:2021-08-09 00:17:07
【问题描述】:

请看一下这段代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchController                                      = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.obscuresBackgroundDuringPresentation = NO;
    self.navigationItem.searchController                       = self.searchController;
    self.definesPresentationContext                            = YES;
    self.searchController.searchBar.placeholder = NSLocalizedString(@"Search", @"search placeholder");
    self.searchController.searchBar.searchTextField.backgroundColor = [UIColor redColor];
}

看起来很简单。我正在尝试将红色背景颜色设置为UISearchTextField。但不幸的是,结果颜色并不完全是红色。当我使用 ColorSync 实用程序检查颜色时,它显示 RGB 为 0.9255、0.0、0.0667,这不是我需要的。当然红色只是一个简化的例子。它适用于任何颜色。实际上我需要用精确的 RGB 设置颜色并且无法这样做。显然,iOS 以某种方式对搜索栏应用了一些色调。我尝试的是:将清晰的颜色用作搜索栏和文本字段的色调以及设置背景图像,但都是徒劳的。这种小色彩永远不会消失。有什么想法吗,伙计们?如何设置精确的颜色?

【问题讨论】:

  • 您是否尝试过查看调试以检查是否存在某种覆盖视图?

标签: ios objective-c uikit uicolor


【解决方案1】:

这里是它会帮助你的解决方案

import UIKit

extension UISearchBar {
    func getTf() -> UITextField? { return value(forKey: "searchField") as? UITextField }
    func setTf(color: UIColor) {
        guard let tf = getTf() else { return }
        switch searchBarStyle {
        case .minimal:
            tf.layer.backgroundColor = color.cgColor
            tf.layer.cornerRadius = 2
        case .prominent: 
              break, 
        default: 
            tf.backgroundColor = color
        @unknown 
            default: break
        }
    }
}

供使用

searchBar.searchBarStyle = .minimal

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2014-03-29
    • 2013-01-07
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多