我尝试在我的应用中实现 iOS 11 中的新搜索栏,但由于某些原因,我无法使搜索栏的颜色与导航栏一起更改。在切换到新样式之前,我在我的 AppDelagate 中使用
我的代码:
UISearchBar.appearance().barTintColor = .DRGreen
改变了颜色,但现在即使进行了这个更改,它仍然保持默认的白色。
错误搜索栏颜色的屏幕截图(抱歉,截图是一个链接,我还没有足够的声望来嵌入图片。)我的代码:
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
}
}