如何在 UISearchController 中自定义 searchBar?

How to customize the searchBar in a UISearchController?

我知道如何设置独立UISearchBar的外观,如下所示。

    let searchField = searchBar.value(forKey: "searchField") as? UITextField

    if let field = searchField {
        field.backgroundColor = UIColor.defaultBackgroundColor
        field.layer.cornerRadius = 15.0
        field.textColor = .white
        field.tintColor = .white

        field.font = UIFont.systemFont(ofSize: fl(13))
        field.layer.masksToBounds = true
        field.returnKeyType = .search
    }

但这在 UISearchController 中不起作用。

我想将占位符和左侧放大镜图标的文字颜色设置为纯白色。 (现在上面好像多了一层彩色)

另外,输入文字现在是黑色的,我也希望它是白色的。

总而言之,我想修改以下属性。
1. 文本框背景色
2. textFiled 占位符文本颜色
3. textFiled 文本颜色
4. textFiled 字体

有人知道怎么做吗?

在 viewDidAppear 中添加以下代码:

            let placeholderString = NSAttributedString(string: "Placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
            field.attributedPlaceholder = placeholderString

            let iconView = field.leftView as! UIImageView
            iconView.image = iconView.image?.withRenderingMode(.alwaysTemplate)
            iconView.tintColor = .white

更新数据:
将这些设置放在 ViewDidAppear() 中确实解决了我的部分问题。
但是当我设置栏的背景颜色时 textfield's background color 发生了变化。
因为 searchBar.barTintColor = .red 在 iOS11 嵌入导航项的 UISearchController 中不起作用,我使用了 searchBar.backgroundColor = .red
这让我很困惑。
那么如何分别改变searchBar的背景和textField的背景呢?

在 viewDidAppear 中添加以下代码:

            let placeholderString = NSAttributedString(string: "Placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
            field.attributedPlaceholder = placeholderString

            let iconView = field.leftView as! UIImageView
            iconView.image = iconView.image?.withRenderingMode(.alwaysTemplate)
            iconView.tintColor = .white

更新 - 以下是自定义 UISearchController 颜色的完整代码:

override func viewDidAppear(_ animated: Bool) {

    //sets navigationbar backgroundColor
    if let navigationbar = self.navigationController?.navigationBar {
        navigationbar.barTintColor = UIColor.magenta
    }

    let searchField = searchController.searchBar.value(forKey: "searchField") as? UITextField

    //sets searchBar backgroundColor
    searchController.searchBar.backgroundColor = .blue

    if let field = searchField {
        field.layer.cornerRadius = 15.0
        //sets text Color
        field.textColor = .brown

        //sets indicator and cancel button Color
        field.tintColor = .green

        field.font = UIFont.systemFont(ofSize: 13)
        field.layer.masksToBounds = true
        field.returnKeyType = .search

        //sets placeholder text Color
        let placeholderString = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
        field.attributedPlaceholder = placeholderString

        //sets icon Color
        let iconView = field.leftView as! UIImageView
        iconView.image = iconView.image?.withRenderingMode(.alwaysTemplate)
        iconView.tintColor = .cyan

        //sets textField backgroundColor
        if let backgroundview = field.subviews.first {
            backgroundview.backgroundColor = UIColor.yellow
        }
    }
}

为搜索栏的文本字段设置attributedPlaceholder

@IBOutlet weak var sbSearchBar: UISearchBar!
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {

    textfield.backgroundColor = UIColor.red
    textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])

    if let leftView = textfield.leftView as? UIImageView {
        leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
        leftView.tintColor = UIColor.white
    }

}

这是结果:

更新:

我想,这可能对你有帮助:

只需在此代码中应用您的颜色组合即可查看。

if #available(iOS 11.0, *) {
            let sc = UISearchController(searchResultsController: nil)
            sc.delegate = self
            let scb = sc.searchBar
            scb.tintColor = UIColor.white
            scb.barTintColor = UIColor.white


            if let textfield = scb.value(forKey: "searchField") as? UITextField {
                //textfield.textColor = // Set text color
                if let backgroundview = textfield.subviews.first {

                    // Background color
                    backgroundview.backgroundColor = UIColor.white

                    // Rounded corner
                    backgroundview.layer.cornerRadius = 10;
                    backgroundview.clipsToBounds = true;

                }
            }

            if let navigationbar = self.navigationController?.navigationBar {
                navigationbar.barTintColor = UIColor.blue
            }
            navigationItem.searchController = sc
            navigationItem.hidesSearchBarWhenScrolling = false

}

结果:

接受的解决方案不适用于 iOS 13,您收到以下错误(使用 Obj-C 代码测试):

Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UISearchBar's _searchField ivar is prohibited. This is an application bug'

但现在您可以选择直接访问 UISearchBar 的 TextField,而无需使用私有 API。

if (@available(iOS 13, *)) {
        self.searchController.searchBar.searchTextField.backgroundColor = [UIColor whiteColor];
        self.searchController.searchBar.searchTextField.tintColor = [UIColor darkGrayColor];
    }
    else {
        UITextField *txfSearchField = [self.searchController.searchBar valueForKey:@"_searchField"];
        UIView *background = txfSearchField.subviews.firstObject;
        background.layer.cornerRadius = 10;
        background.clipsToBounds = true;
        background.backgroundColor=[UIColor whiteColor];

        txfSearchField.tintColor=[UIColor darkGrayColor];
        txfSearchField.textColor = [UIColor redColor];
    }