Create a UISearchController

If the searchResultsController is passed in nil, that means the results of the search are displayed in the current controller, and NOW I have it displayed in the searchResultVC

/ / create searchResultVC
let searchResultVC = UIViewController(a)// Set the background color to red
searchResultVC.view.backgroundColor = UIColor.red
let searchController = UISearchController(searchResultsController: searchResultVC)
// Set the background color
searchController.view.backgroundColor = UIColor (red: 0.97, green: 0.97, blue: 0.97, alpha: 1.0)
// The default value is YES, which sets whether the background is displayed when the search begins
// searchController.dimsBackgroundDuringPresentation = false
// The default is YES, which controls whether to hide the navigation bar during search
// searchController.hidesNavigationBarDuringPresentation = false

// A strong reference to the searchController is required
self.searchController = searchController

// Set the search box view to tableHeaderView of tableView
tableView.tableHeaderView = searchController.searchBar
Copy the code

Set the search box

/ / search box
let bar = searchController.searchBar
/ / style
bar.barStyle = .default
// Set the color of the cursor and cancel button
bar.tintColor = RGBA(r: 0.12, g: 0.74, b: 0.13, a: 1.00)
// Set the proxy
bar.delegate = self
Copy the code

Remove the background

// Remove the background and the top and bottom lines
bar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
Copy the code

Add the right voice button

// Voice on the right
bar.showsBookmarkButton = true
bar.setImage(#imageLiteral(resourceName: "VoiceSearchStartBtn"), for: .bookmark, state: .normal)
Copy the code

Listen for voice button clicks

// MARK:- UISearchBarDelegate
extension LXFContactViewController: UISearchBarDelegate {
    func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
        LXFLog("Hit the voice button.")}}Copy the code

The effect

The main code

The code below is the main setup for the searchBar

let commonBgColor = RGBA(r: 0.94, g: 0.94, b: 0.96, a: 1.00)
searchBar.barTintColor = commonBgColor

/ / search box
searchBar.barStyle = .default
searchBar.tintColor = RGBA(r: 0.12, g: 0.74, b: 0.13, a: 1.00)
// Remove the top and bottom lines
searchBar.setBackgroundImage(commonBgColor.trans2Image(), for: .any, barMetrics: .default)
// Voice on the right
searchBar.showsBookmarkButton = true
searchBar.setImage(#imageLiteral(resourceName: "VoiceSearchStartBtn"), for: .bookmark, state: .normal)
Copy the code
extension UIColor {
    func trans2Image(a) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext() context? .setFillColor(self.cgColor) context? .fill(rect)let theImage = UIGraphicsGetImageFromCurrentImageContext(a)UIGraphicsEndImageContext(a)return theImage ?? UIImage()}}Copy the code

Attached is related project: Swift 3.0 high imitation wechat