UISearchController searchBar iOS 11中文本字段的背景色

问题描述

我想使用以下代码更改嵌入在iOS 11的UISearchController中的searchBar中的文本字段的背景颜色:

class ViewController: UIViewController {
private var searchController: UISearchController!
    
    private var data: [String] = []
    
    private var filtered: [String] = []
    
    private var tableView = UITableView(frame: .zero,style: .plain)

    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        for i in 1...100 {
            self.data.append("row \(i)")
        }
        self.filtered = data
        
        let src = SearchResultController()
        self.searchController = UISearchController(searchResultsController: src)
        self.searchController.delegate = self
        self.searchController.searchResultsUpdater = src
        
        self.searchController.searchBar.textField?.subviews.first?.backgroundColor = UIColor(red:0.25,green:0.37,blue:0.53,alpha:1)
        
        if #available(iOS 13.0,*) {
            self.searchController.showsSearchResultsController = true
        }
        self.navigationItem.title = "Search"
        self.navigationItem.hidesSearchBarWhenScrolling = false
        self.navigationItem.searchController = searchController

        self.setupNavigationBar()
        self.setupTableView()
        
        definesPresentationContext = true
    }
}

所以当我写这样的代码

self.searchController.searchBar.textField.backgroundColor = UIColor(red:0.25,alpha:1)

什么都没有改变。

我写的时候

self.searchController.searchBar.textField?.subviews.first?.backgroundColor = UIColor(red:0.25,alpha:1)

我有这样的东西。问题是文本文件太暗了。

enter image description here

当我写这篇文章时。阿尔法= 0.5

self.searchController.searchBar.textField?.subviews.first?.backgroundColor = UIColor(red:0.25,alpha:0.5)

我有这个。这就是我想要的。

enter image description here

所以我的问题是我做错了什么?还有什么合适的方法来设置嵌入在UISearchController中的搜索栏中的文本文件的背景色?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...