10

的文档UISearchController说您可以覆盖- searchBar以提供自定义子类UISearchBar供控制器使用。自定义搜索栏确实被使用了,并且它自己的委托方法被正确调用,但是UISearchResultsUpdating当搜索栏发生变化时,该方法不再被调用。我是否需要手动进行很多接线,或者我是否缺少一些东西来让控制器像使用本机提供的搜索栏一样工作?

4

4 回答 4

4

在您的自定义 UISearchController 类中覆盖 SearchBar getter,它必须返回您的自定义 SearchBar 并且它必须已经初始化,然后您仅在 UISearchController 初始化之后设置它的属性,这样所有 UISearchController 功能都将保留:

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)

    }
}

这是我的自定义搜索栏:

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
于 2016-03-25T11:25:18.277 回答
3

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

于 2014-09-19T18:53:41.633 回答
2

当您继承 UISearchController 时,您可以在 getter 中自定义 UISearchBar(setter 不存在)。

示例 - 在子类实现中:

-(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;
}

希望这可以帮助某人。

于 2015-01-12T20:10:43.207 回答
-1

我认为它应该表现得像那样。

这是来自 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:) 所做的只是返回您的搜索控制器,以便您可以访问它的搜索栏。

您可以通过自定义搜索栏委托方法来做到这一点。

于 2014-11-11T17:14:57.167 回答