gpt4 book ai didi

uisearchbar - iOS 11 UISearchBar 不接受 tintColor

转载 作者:行者123 更新时间:2023-12-04 03:11:27 27 4
gpt4 key购买 nike

我尝试在我的应用程序中实现 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

}

}

最佳答案

您可以使用如下方法更改 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)
}

关于uisearchbar - iOS 11 UISearchBar 不接受 tintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46491930/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com