如何在searchcontroller中更改uitextfield颜色?

时间:2017-12-14 13:08:47

标签: ios swift uinavigationcontroller ios11 uisearchcontroller

enter image description here

我在导航栏中嵌入了SearchController。如何更改UITextField颜色将搜索字符串保持为白色的那个。?

1 个答案:

答案 0 :(得分:2)

这将帮助您实现您想要的。只需在此代码中应用颜色组合,然后查看。

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

}

<强>结果:

enter image description here