【问题标题】:how to change uitextfield color in searchcontroller?如何更改搜索控制器中的 uitextfield 颜色?
【发布时间】:2017-12-14 13:08:46
【问题描述】:

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

【问题讨论】:

  • 您想将文本字段背景更改为绿色并搜索字符串白色

标签: ios swift uinavigationcontroller ios11 uisearchcontroller


【解决方案1】:

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

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

}

结果:

【讨论】:

  • 当我点击搜索时,它会使屏幕变得模糊(松散焦点),就像在动画中看到的那样。如何解决。 BarTintColor 会解决这个问题吗?
  • 哦,我没有标记那个。给我一些时间。让我检查一下并提供您可行的解决方案。
  • searchController.obscuresBackgroundDuringPresentation = false 设置此属性成功了。
  • 您真的很棒 - 欢迎您使用建议的输入更新和改进此答案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多