閑的沒事寫了寫swift,記錄一下比較坑的控件P战ā呀邢!
聲明
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 = "請輸入關(guān)鍵字"
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("請輸入搜搜內(nèi)容幕与!"), delay:2)
return;
}
// 調(diào)用搜索方法
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
}
}
}
``