UISearchBarSearchField BackgroundView color

前端 未结 3 1234
南旧
南旧 2021-01-28 11:39

I am trying to change the background color of the search bar text field and i have searched and tried lots of solutions but those are not working.

So, please anyone can

相关标签:
3条回答
  • 2021-01-28 12:01

    You can try this

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

    Output

    Edit : Full Code

        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)
    
    0 讨论(0)
  • 2021-01-28 12:12

    After a lot more search I found the correct answer that is working for me.

    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;
                }
            }
    
      }
    
    0 讨论(0)
  • 2021-01-28 12:20

    Since iOS 13:

            if #available(iOS 13.0, *) {
                searchController.searchBar.searchTextField.backgroundColor = .red
                searchController.searchBar.searchTextField.tintColor = .yellow
            }
    
    0 讨论(0)
提交回复
热议问题