我已尝试将iOS 11中的新搜索栏实施到我的应用中,但出于某种原因,我无法通过导航栏更改搜索栏颜色。在切换到新样式之前,我在AppDelagate中使用UISearchBar.appearance().barTintColor = .DRGreen
更改了颜色,但现在即使进行了更改,它仍然是默认的白色。
Screen Shot of incorrect search bar color(抱歉截图是一个链接,我还没有足够的声誉来嵌入图片。)
我的代码:
import UIKit
class DirectoryTC: UITableViewController, UISearchResultsUpdating {
var teachers = [
Redacted information
]
var filteredTeachers = [Teacher]()
var selectedTeacher = Teacher(name: "", email: "", roomNumber: "", website: "")
var searchController: UISearchController!
var resultsController = UITableViewController()
var searchText = ""
override func viewDidLoad()
{
super.viewDidLoad()
self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self
if #available(iOS 11, *)
{
searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.text = searchText
self.navigationItem.searchController = searchController
self.navigationItem.searchController?.searchBar.tintColor = UIColor.DRGreen
searchController.isActive = true
}else
{
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.layer.borderWidth = 1
self.searchController.searchBar.layer.borderColor = UIColor.DRGreen.cgColor
present(searchController, animated: true, completion: nil)
}
definesPresentationContext = true
//Make Search Background Green
//tableView.backgroundView = UIView()
//tableView.backgroundView?.backgroundColor = .DRGreen
//Remove Navigation Bar Border
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
//Add in the DR LOGO on top Bar
addNavBarImage()
}
func updateSearchResults(for searchController: UISearchController) {
self.filteredTeachers = self.teachers.filter{ (teacher:Teacher) -> Bool in
if teacher.name.lowercased().contains(searchText.lowercased())
{
return true
}else
{
return false
}
}
self.resultsController.tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if tableView == self.tableView
{
return self.teachers.count
}else
{
return self.filteredTeachers.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell()
let teacher : Teacher
if tableView == self.tableView
{
teacher = teachers[indexPath.row]
}else
{
teacher = filteredTeachers[indexPath.row]
}
cell.textLabel?.text = teacher.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let teacher : Teacher
if tableView == self.tableView
{
teacher = teachers[indexPath.row]
selectedTeacher = teacher
}else
{
teacher = filteredTeachers[indexPath.row]
selectedTeacher = teacher
}
self.performSegue(withIdentifier: "teacherDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as? DirectoryDetailVC
vc?.name = selectedTeacher.name
vc?.email = selectedTeacher.email
vc?.roomNumber = selectedTeacher.roomNumber
vc?.website = selectedTeacher.website
}
func addNavBarImage() {
let navBarController = navigationController!
let image = #imageLiteral(resourceName: "DRNavigationBarLogo")
let imageView = UIImageView(image: image)
let bannerWidth = navBarController.navigationBar.frame.size.width
let bannerHeight = navBarController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - image.size.width / 2
let bannerY = bannerHeight / 2 - image.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
}
答案 0 :(得分:0)
您可以使用以下方法更改UISearchBar背景颜色:
func setBackground(searchBar: UISearchBar) {
for subView in searchBar.subviews {
for view in subView.subviews {
if view.isKind(of: NSClassFromString("UISearchBarBackground")!) {
guard let imageView = view as? UIImageView else {
print("cast exception ui image view")
continue
}
imageView.image = nil
imageView.backgroundColor = UIColor.DRGreen
return
}
}
}
}
并在初始化UISearchControl后调用此方法。 例如:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
setBackground(searchBar: self.searchController.searchBar)
}