iOS 11自定义导航栏中的搜索栏

iOS 11自定义导航栏中的搜索栏,第1张

概述我想在iOS 11搜索栏中嵌入导航栏时更改文本和图标的颜色.所以占位符文本,搜索文本和搜索图标. if #available(iOS 11.0, *) { navigationController?.navigationBar.prefersLargeTitles = false let searchController = UISearchController(searchRes 我想在iOS 11搜索栏中嵌入导航栏时更改文本和图标的颜色.所以占位符文本,搜索文本和搜索图标.

if #available(iOS 11.0,*) {    navigationController?.navigationbar.prefersLargeTitles = false    let searchController = UISearchController(searchResultsController: nil)    navigationItem.searchController = searchController    navigationItem.hIDesSearchbarWhenScrolling = false    searchController.searchbar.placeholder = "Suchen"    searchController.searchbar.tintcolor = .white}

正如您在图像中看到的那样,文本在深蓝色背景上呈灰色,看起来很难看.我希望文字和图标至少是白色的. (改变蓝色背景颜色也行不通,见my other question)

唯一有效的方法是改变闪烁光标的颜色和“取消”按钮,这是通过.tintcolor属性完成的.

似乎在iOS 10及以下版本中工作的解决方案在iOS 11中似乎不再起作用,因此请仅发布您在iOS 11中工作的解决方案.谢谢.

也许我错过了iOS 11中关于这种“自动样式”的观点.任何帮助都表示赞赏.

解决方法 我刚刚发现了如何设置其余部分:(在布兰登的帮助下,谢谢!)

“取消”文字:

searchController.searchbar.tintcolor = .white

搜索图标:

searchController.searchbar.setimage(UIImage(named: "my_search_icon"),for: UISearchbarIcon.search,state: .normal)

清晰的图标:

searchController.searchbar.setimage(UIImage(named: "my_search_icon"),for: UISearchbarIcon.clear,state: .normal)

搜索文字:

UITextFIEld.appearance(whenContainedInInstancesOf: [UISearchbar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundcolor.rawValue: UIcolor.white]

感谢@Brandon的帮助!

占位符:

UITextFIEld.appearance(whenContainedInInstancesOf: [UISearchbar.self]).attributedplaceholder = NSAttributedString(string: "placeholder",attributes: [NSAttributedStringKey.foregroundcolor: 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 = UIcolor.blue        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}

摘自here.

总结

以上是内存溢出为你收集整理的iOS 11自定义导航栏中的搜索栏全部内容,希望文章能够帮你解决iOS 11自定义导航栏中的搜索栏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1019776.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存