UISearchBarSearchField 背景视图颜色

Posted

技术标签:

【中文标题】UISearchBarSearchField 背景视图颜色【英文标题】:UISearchBarSearchField BackgroundView color 【发布时间】:2019-09-27 04:01:42 【问题描述】:

我正在尝试更改搜索栏文本字段的背景颜色,我已经搜索并尝试了很多解决方案,但这些都不起作用。

所以,请任何人都可以告诉我确切的解决方案。我们如何更改搜索栏文本字段的背景颜色?

/// used to prepare searchController inside navigation.
private func prepareNavigationSearchControllerSetup() 
    self.title = AppConstant.kContacts
    let search = UISearchController(searchResultsController: nil)
    search.searchResultsUpdater = self
    search.searchBar.cornerRadius = 10.0
    search.searchBar.textField?.backgroundColor = .red
    search.searchBar.textField?.tintColor = .yellow
    self.navigationItem.searchController = search
    self.navigationItem.hidesSearchBarWhenScrolling = false


extension UISearchBar 

    var textField: UITextField? 
        let subViews = subviews.flatMap  $0.subviews 
        return (subViews.filter  $0 is UITextField ).first as? UITextField
    

【问题讨论】:

【参考方案1】:

经过大量搜索后,我找到了适合我的正确答案。

if #available(ios 11.0, *) 
        if let textfield = search.searchBar.value(forKey: "searchField") as? UITextField 
            textfield.textColor = UIColor.blue

            if let backgroundview = textfield.subviews.first 
                backgroundview.backgroundColor = UIColor.white
                backgroundview.layer.cornerRadius = 10;
                backgroundview.clipsToBounds = true;
            
        

  

【讨论】:

【参考方案2】:

你可以试试这个

UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue

输出

编辑:完整代码

    var searchController = UISearchController()
    let resultsTableController = Storyboard.Home.instantiateViewController(withIdentifier: "GlobalTableVC") as! GlobalTableVC
    resultsTableController.tableView.delegate = self
    resultsTableController.tableView.dataSource = self

    resultsTableController.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SearchCell")

    searchController = UISearchController(searchResultsController: resultsTableController)
    searchController.searchBar.placeholder = "Search"
    searchController.dimsBackgroundDuringPresentation = true
    searchController.searchBar.sizeToFit()
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.keyboardType = UIKeyboardType.alphabet
    searchController.searchBar.tintColor = UIColor.white
    searchController.searchBar.barTintColor = UIColor(hexString: "EB033B")

    UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).backgroundColor = .yellow
    UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .blue


    searchController.searchBar.delegate = self
    searchController.delegate = self
    searchController.searchResultsUpdater = self

    present(searchController, animated: true, completion: nil)

【讨论】:

你能告诉我上面的代码应该放在哪里吗? @AhemadabbasVagh 在prepareNavigationSearchControllerSetup。我认为您的 search.searchBar.textField?.tintColorsearch.searchBar.textField?.backgroundColor 不起作用,因此您可以在此处替换行 @AhemadabbasVagh 检查完整代码。导航栏中有按钮,在 btnaction 上我调用了此代码。您的要求可能会有所不同,您需要进行相应的编码。【参考方案3】:

自 iOS 13 起:

        if #available(iOS 13.0, *) 
            searchController.searchBar.searchTextField.backgroundColor = .red
            searchController.searchBar.searchTextField.tintColor = .yellow
        

【讨论】:

以上是关于UISearchBarSearchField 背景视图颜色的主要内容,如果未能解决你的问题,请参考以下文章

Android设置背景图片和背景色

批量视频处理批量视频编辑背景移除/去背景/换背景/抠图/抠像代码/实时抠图实时抠像人像去背景背景消除摄像头背景移除

深入理解CSS背景

css可以添加背景颜色和背景图片,如果想要设置背景的图片固定而不是滚动需要?

CSS DIV嵌套,父DIV的背景图覆盖子DIV的背景图,还是子DIV的背景图覆盖父DIV的背景图

HTML背景颜色和背景图片