UIKitCatalog/UIKitCatalog/CustomSearchBarViewController.swift
2020-06-17 11:00:09 -08:00

61 lines
1.6 KiB
Swift
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
See LICENSE folder for this samples licensing information.
Abstract:
A view controller that demonstrates how to customize a `UISearchBar`.
*/
import UIKit
class CustomSearchBarViewController: UIViewController {
// MARK: - Properties
@IBOutlet weak var searchBar: UISearchBar!
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
configureSearchBar()
}
// MARK: - Configuration
func configureSearchBar() {
searchBar.showsCancelButton = true
searchBar.showsBookmarkButton = true
searchBar.tintColor = UIColor.systemPurple
searchBar.backgroundImage = UIImage(named: "search_bar_background")
// Set the bookmark image for both normal and highlighted states.
let bookImage = UIImage(systemName: "bookmark")
searchBar.setImage(bookImage, for: .bookmark, state: .normal)
let bookFillImage = UIImage(systemName: "bookmark.fill")
searchBar.setImage(bookFillImage, for: .bookmark, state: .highlighted)
}
}
// MARK: - UISearchBarDelegate
extension CustomSearchBarViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("The custom search bar keyboard \"Search\" button was tapped.")
searchBar.resignFirstResponder()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("The custom search bar \"Cancel\" button was tapped.")
searchBar.resignFirstResponder()
}
func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
print("The custom \"bookmark button\" inside the search bar was tapped.")
}
}