JCSearchController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // JCSearchView.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/3/22.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. @objc public protocol JCSearchControllerDelegate: NSObjectProtocol {
  10. @objc optional func didEndEditing(_ searchBar: UISearchBar)
  11. }
  12. class JCSearchController: UISearchController {
  13. weak var searchControllerDelegate: JCSearchControllerDelegate?
  14. override init(searchResultsController: UIViewController?) {
  15. super.init(searchResultsController: searchResultsController)
  16. _init()
  17. }
  18. required init?(coder aDecoder: NSCoder) {
  19. fatalError("init(coder:) has not been implemented")
  20. }
  21. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  22. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  23. _init()
  24. }
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. }
  28. override func viewDidDisappear(_ animated: Bool) {
  29. super.viewDidDisappear(animated)
  30. let frame = searchBar.frame
  31. if frame.origin.y > 0 && frame.origin.y < 20 {
  32. searchBar.frame = CGRect(x: frame.origin.x, y: 20, width: frame.size.width, height: 31)
  33. } else {
  34. searchBar.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: 31)
  35. }
  36. }
  37. override func viewDidLayoutSubviews() {
  38. super.viewDidLayoutSubviews()
  39. searchBar.layer.borderColor = UIColor.white.cgColor
  40. searchBar.layer.borderWidth = 1
  41. searchBar.layer.masksToBounds = true
  42. let frame = searchBar.frame
  43. if frame.origin.y > 0 && frame.origin.y < 20 {
  44. searchBar.frame = CGRect(x: frame.origin.x, y: 20, width: frame.size.width, height: 31)
  45. } else {
  46. searchBar.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: 31)
  47. }
  48. }
  49. private func _init() {
  50. automaticallyAdjustsScrollViewInsets = true
  51. dimsBackgroundDuringPresentation = false
  52. hidesNavigationBarDuringPresentation = true
  53. //definesPresentationContext = true
  54. //extendedLayoutIncludesOpaqueBars = true
  55. searchBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 31)
  56. if #available(iOS 11.0, *) {
  57. searchBar.setPositionAdjustment(UIOffset(horizontal: 0, vertical: 3), for: .search)
  58. searchBar.searchTextPositionAdjustment = UIOffset(horizontal: 5, vertical: 3)
  59. }
  60. searchBar.barStyle = .default
  61. searchBar.backgroundColor = .white
  62. searchBar.barTintColor = .white
  63. searchBar.delegate = self
  64. searchBar.autocapitalizationType = .none
  65. searchBar.placeholder = "搜索"
  66. searchBar.layer.borderColor = UIColor.white.cgColor
  67. searchBar.layer.borderWidth = 1
  68. searchBar.layer.masksToBounds = true
  69. searchBar.setSearchFieldBackgroundImage(UIImage.createImage(color: .white, size: CGSize(width: UIScreen.main.bounds.size.width, height: 31)), for: .normal)
  70. }
  71. override var preferredStatusBarStyle: UIStatusBarStyle {
  72. return .lightContent
  73. }
  74. }
  75. extension JCSearchController: UISearchBarDelegate {
  76. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  77. searchControllerDelegate?.didEndEditing?(searchBar)
  78. }
  79. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  80. searchBar.showsCancelButton = true
  81. for view in (searchBar.subviews.first?.subviews)! {
  82. if view is UIButton {
  83. let cancelButton = view as! UIButton
  84. cancelButton.setTitleColor(UIColor(netHex: 0x999999), for: .normal)
  85. if #available(iOS 11.0, *) {
  86. cancelButton.titleEdgeInsets = UIEdgeInsets(top: 8, left: 0, bottom: 0, right: 0)
  87. }
  88. break
  89. }
  90. }
  91. }
  92. }