Swift 学习之 UISearchController

效果图:

 

   func setSearchControllerView() -> Void {
        
        /** 设置 searchResultController 并初始化 UISearchController*/
        let naviSearchResultVC = UINavigationController(rootViewController: SearchResultViewController())
        self.searchController = UISearchController(searchResultsController: naviSearchResultVC)
        
        /** 设置 searchBar 的位置,即 frame */
        self.searchController?.searchBar.frame = CGRect.init(x: (self.searchController?.searchBar.frame.origin.x)!, y: (self.searchController?.searchBar.frame.origin.x)! + 10, width: (self.searchController?.searchBar.frame.size.width)!, height: 44);
        
        /** 是否显示蒙版,true 显示,false 不显示 */
        self.searchController?.dimsBackgroundDuringPresentation = true;
        
        /** 遵循 UISearchResultsUpdating 的代理 */
        self.searchController?.searchResultsUpdater = self;
        
        /** 遵循 UISearchBarDelegate 的代理 */
        self.searchController?.searchBar.delegate = self
        
        /** 设置 searchBar 的 tintColor,可以设置光标颜色和取消(Cancel)btn 的字体颜色 */
        self.searchController?.searchBar.tintColor = appDefaultColor
        
        /** 设置 searchBar 的 placeholder */
        self.searchController?.searchBar.placeholder = "搜索"
        
        /** NavigationBar 是否隐藏 */
        self.searchController?.hidesNavigationBarDuringPresentation = true
        
        /** 设置 searchBar 的 barTintColor*/
        self.searchController?.searchBar.barTintColor = UIColor.white
        
        /** 设置 searchBar 的背景颜色 */
        self.searchController?.searchBar.backgroundColor = UIColor.cyan
        
        
        self.searchController?.searchBar.viewWithTag(1)?.removeFromSuperview()
        let bgView = UIView(frame: (self.searchController?.searchBar.bounds)!)
        bgView.backgroundColor = UIColor.white
        self.searchController?.searchBar.insertSubview(bgView, at: 1)
        
        /** 设置搜索框的背景颜色和输入字体颜色 */
        let textField = self.searchController?.searchBar.value(forKey: "searchField") as! UITextField
        textField.backgroundColor = UIColor(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1)
        
        
        /** 添加 searchBar 到 tableView 的 tableHeaderView上 */
        self.tableView.tableHeaderView = self.searchController?.searchBar
        
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        
        /** 显示 CancelButton */
        searchBar.setShowsCancelButton(true, animated: true)
        
        /** 改变 CancelButton 的 文字 */
        var cancelButton: UIButton?
        let topView: UIView = searchBar.subviews[0]
        for view in topView.subviews  {
            if view.isKind(of: NSClassFromString("UIButton")!){
                cancelButton =  view as? UIButton
            }
        }
        if (cancelButton != nil) {
            cancelButton?.setTitle("取消", for: UIControlState.normal)
        }
    }
    
    @objc(updateSearchResultsForSearchController:) func updateSearchResults(for searchController: UISearchController){
        //搜索结果
    }
UISearchController 代码

 

posted @ 2016-12-06 18:16  Now,OnMyWay  阅读(322)  评论(0编辑  收藏  举报