使用UISearchController自定义UISearchBar

UISearchController的文档说,你可以重载- searchBar提供一个UISearchBar的自定义子类供控制器使用。 自定义search栏没有被使用,并且它自己的委托方法调用正确,但是当search栏改变时, UISearchResultsUpdating方法不再被调用。 我是否需要手动进行大量的接线工作,或者是否有些东西让控制器performance得与本机提供的search栏一样?

这是一个已知的错误。 不幸的是,没有涉及私有API的解决方法。

当你inheritanceUISearchController时,你可以在getter(setter不存在)中自定义UISearchBar。

例子 – 在子类实现中:

 -(UISearchBar*)searchBar{ UISearchBar *baseSearchBar = [super searchBar]; if (baseSearchBar.showsScopeBar) { baseSearchBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 88); }else{ baseSearchBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44); } return baseSearchBar; } 

希望这有助于某人。

重写自定义UISearchController类中的SearchBar获取器,它必须返回自定义SearchBar,并且必须已经初始化它,然后在UISearchController初始化之后设置它的属性,这样所有的UISearchControllerfunction都保留下来:

 public class DSearchController: UISearchController { private var customSearchBar = DSearchBar() override public var searchBar: UISearchBar { get { return customSearchBar } } required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) } public init(searchResultsController: UIViewController?, searchResultsUpdater: UISearchResultsUpdating?, delegate: UISearchControllerDelegate?, dimsBackgroundDuringPresentation: Bool, hidesNavigationBarDuringPresentation: Bool, searchBarDelegate: UISearchBarDelegate?, searchBarFrame: CGRect?, searchBarStyle: UISearchBarStyle, searchBarPlaceHolder: String, searchBarFont: UIFont?, searchBarTextColor: UIColor?, searchBarBarTintColor: UIColor?, // Bar background searchBarTintColor: UIColor) { // Cursor and bottom line super.init(searchResultsController: searchResultsController) self.searchResultsUpdater = searchResultsUpdater self.delegate = delegate self.dimsBackgroundDuringPresentation = dimsBackgroundDuringPresentation self.hidesNavigationBarDuringPresentation = hidesNavigationBarDuringPresentation customSearchBar.setUp(searchBarDelegate, frame: searchBarFrame, barStyle: searchBarStyle, placeholder: searchBarPlaceHolder, font: searchBarFont, textColor: searchBarTextColor, barTintColor: searchBarBarTintColor, tintColor: searchBarTintColor) } } 

这是我的自定义search栏:

 public class DSearchBar: UISearchBar { var preferredFont: UIFont? var preferredTextColor: UIColor? init(){ super.init(frame: CGRect.zero) } func setUp(delegate: UISearchBarDelegate?, frame: CGRect?, barStyle: UISearchBarStyle, placeholder: String, font: UIFont?, textColor: UIColor?, barTintColor: UIColor?, tintColor: UIColor?) { self.delegate = delegate self.frame = frame ?? self.frame self.searchBarStyle = searchBarStyle self.placeholder = placeholder self.preferredFont = font self.preferredTextColor = textColor self.barTintColor = barTintColor ?? self.barTintColor self.tintColor = tintColor ?? self.tintColor self.bottomLineColor = tintColor ?? UIColor.clearColor() sizeToFit() // translucent = false // showsBookmarkButton = false // showsCancelButton = true // setShowsCancelButton(false, animated: false) // customSearchBar.backgroundImage = UIImage() } required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } let bottomLine = CAShapeLayer() var bottomLineColor = UIColor.clearColor() override public func layoutSubviews() { super.layoutSubviews() for view in subviews { if let searchField = view as? UITextField { setSearchFieldAppearance(searchField); break } else { for sView in view.subviews { if let searchField = sView as? UITextField { setSearchFieldAppearance(searchField); break } } } } bottomLine.path = UIBezierPath(rect: CGRectMake(0.0, frame.size.height - 1, frame.size.width, 1.0)).CGPath bottomLine.fillColor = bottomLineColor.CGColor layer.addSublayer(bottomLine) } func setSearchFieldAppearance(searchField: UITextField) { searchField.frame = CGRectMake(5.0, 5.0, frame.size.width - 10.0, frame.size.height - 10.0) searchField.font = preferredFont ?? searchField.font searchField.textColor = preferredTextColor ?? searchField.textColor //searchField.backgroundColor = UIColor.clearColor() //backgroundImage = UIImage() } } 

初始示例:

 searchController = DSearchController(searchResultsController: ls, searchResultsUpdater: self, delegate: self, dimsBackgroundDuringPresentation: true, hidesNavigationBarDuringPresentation: true, searchBarDelegate: ls, searchBarFrame: CGRectMake(0.0, 0.0, SCREEN_WIDTH, 44.0), searchBarStyle: .Minimal, searchBarPlaceHolder: NSLocalizedString("Search a location...", comment: ""), searchBarFont: nil, searchBarTextColor: nil, searchBarBarTintColor: UIColor.whiteColor(), searchBarTintColor: iconsColor) searchController.searchBar.keyboardAppearance = .Dark definesPresentationContext = true tableView.tableHeaderView = searchController.searchBar 

我认为这应该是这样的。

这是来自UISearchController.h

 // You are free to become the search bar's delegate to monitor for text changes and button presses. @property (nonatomic, retain, readonly) UISearchBar *searchBar; 

所有委托方法(updateSearchResultsForSearchController :)所做的是返回您的search控制器,以便您可以访问其search栏。

你可以通过你自定义的search栏委托方法来做到这一点。