ZLUISearchBar.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // ZLUISearchBar.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2017/3/23.
  6. // Copyright © 2017年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. class ZLUISearchBar: UISearchBar {
  10. override init(frame: CGRect) {
  11. super.init(frame: frame)
  12. commonInit()
  13. }
  14. required init?(coder aDecoder: NSCoder) {
  15. fatalError("init(coder:) has not been implemented")
  16. }
  17. override func awakeFromNib() {
  18. super.awakeFromNib()
  19. commonInit()
  20. }
  21. private func commonInit(){
  22. self.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
  23. self.barTintColor = navbar_tint_color
  24. if let searchField = self.value(forKey: "searchField") as? UITextField {
  25. searchField.backgroundColor = UIColor.white
  26. searchField.layer.cornerRadius = 14
  27. searchField.layer.borderColor = UIColor.gray.cgColor
  28. searchField.layer.borderWidth = 1
  29. searchField.layer.masksToBounds = true
  30. }
  31. self.setCancelButtonTitle("取消")
  32. }
  33. private func setCancelButtonTitle(_ title:String){
  34. UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = title
  35. }
  36. // //3. 设置按钮文字和颜色
  37. // [self.customSearchBar fm_setCancelButtonTitle:@"取消"];
  38. // self.customSearchBar.tintColor = [UIColor colorWithRed:86/255.0 green:179/255.0 blue:11/255.0 alpha:1];
  39. // //修正光标颜色
  40. // [searchField setTintColor:[UIColor blackColor]];
  41. //
  42. // //其中fm_setCancelButtonTitle是我写的UISearchBar一个分类的方法
  43. // - (void)fm_setCancelButtonTitle:(NSString *)title {
  44. // if (IS_IOS9) {
  45. // [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:title];
  46. // }else {
  47. // [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:title];
  48. // }
  49. // }
  50. }