简体   繁体   English

如何更改搜索控制器中的 uitextfield 颜色?

[英]how to change uitextfield color in searchcontroller?

在此处输入图像描述

i have embedded SearchController in navigation bar.我在导航栏中嵌入了 SearchController。 How to Change UITextField Color The one which Holds search String To White.?如何将 UITextField 颜色更改为白色。?

This will help you to achieve, what you want. 这将帮助您实现您想要的。 Just apply your color combination in this code and see. 只需在此代码中应用颜色组合即可。

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

}

Result: 结果:

在此输入图像描述

Excellent answer, in my novice opinion we could also try using the member of the UI Search bar called searchTextField which is ultimately a subclass of UITextField but we can get away without using很好的答案,在我的新手看来,我们也可以尝试使用名为 searchTextField 的 UI 搜索栏成员,它最终是 UITextField 的子类,但我们可以不使用

if let textfield = scb.value(forKey: "searchField") as? UITextField
//we could use 
let textField = scb.searchTextField
followed by the same changes made to the views layer, cornerRadius etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM