mirror of
https://github.com/samsonjs/UIKitCatalog.git
synced 2026-03-25 08:55:51 +00:00
61 lines
1.6 KiB
Swift
Executable file
61 lines
1.6 KiB
Swift
Executable file
/*
|
||
See LICENSE folder for this sample’s 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.")
|
||
}
|
||
|
||
}
|