swift- UISearchController的基本使用

闲的没事写了写swift,记录一下比较坑的控件!!
声明

var jcSearchView =UISearchController()

添加 UISearchController

func jcAddSearchViewController() ->Void{
        let vc = JFSearchResultViewController()
        jcSearchView=UISearchController.init(searchResultsController: vc)
        jcSearchView.delegate = self
        jcSearchView.searchResultsUpdater = self
        jcSearchView.searchBar.delegate = self
        jcSearchView.searchBar.placeholder = "请输入关键字"
        self.jcSearchView.dimsBackgroundDuringPresentation = true;
        self.jcSearchView.searchBar.sizeToFit()
        jcTableView.tableHeaderView = jcSearchView.searchBar
        //push到下一个view controller之后search bar不会仍留在界面上
        self.definesPresentationContext = true;
        jcSearchView.searchBar.tintColor = JF_MAIN_COLOR
        jcSearchView.searchBar.isTranslucent = false
        jcSearchView.searchBar.barTintColor = UIColor.white
// 修改 searchBar 文本输入背景
       let textField:UITextField=jcSearchView.searchBar.value(forKey:"_searchField")as!UITextField
       textField.backgroundColor=UIColor.init(red:235/255, green:235/255, blue:235/255, alpha:1)
    }

UISearchController 代理方法,通过代理方法进行修改一些自定义样式等

earchController.searchBar.showsCancelButton=true
letbtn:UIButton? = searchController.searchBar.value(forKey:"_cancelButton")as?UIButton
通过以上代码在代理方法中获取到searchBar中的取消按钮修改成中文显示

extension JFHanleProViewController:UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate{

    public funcwillDismissSearchController(_searchController:UISearchController){

    }
    func updateSearchResults(for searchController:UISearchController) {
        searchController.searchBar.showsCancelButton=true
        letbtn:UIButton? = searchController.searchBar.value(forKey:"_cancelButton")as?UIButton
        ifbtn !=nil{
            btn!.setTitle("取消", for: UIControlState.normal)
        }
    }

    public fun csearchBarSearchButtonClicked(_searchBar:UISearchBar){
        if searchBar.text == nil{
            HUD.flash(.label("请输入搜搜内容!"), delay:2)
            return;
        }

// 调用搜索方法

        JFAPI.jcGetFlowPro(jcHandelModel.FlowType, jcCompleted, searchBar.text!, 0, { (jsonData) in
            let jcCont = self.jcSearchView.searchResultsController as! JFSearchResultViewController
            var searchArray =Array()
            for obj in self.jcArray{
                searchArray.append(obj)
            }
            jcCont.jcSearchArray= searchArrayasNSArray
            jcCont.jcSearchType = JFSearchType.hanle
            jcCont.tableView.reloadData()
        }) { (errString, errCode) in
        }
    }

}
``

你可能感兴趣的:(swift- UISearchController的基本使用)