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

}
``
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 怎样的人才是健康的?这是心理学,特别是心理咨询与治疗必然要涉及到一个基本问题。 如果你去找一位资深的精神分析师去做...
    宇宙的恬缘阅读 4,472评论 0 0
  • 过年的春节,外来工人们纷纷提着自己的行李箱,大包小包,穿着新感时尚潮流的衣服回家和家中的父母团圆。 我也不曾例外,...
    骤雨落舍阅读 2,372评论 0 2
  • 【幼儿说】原创,转载请标出处 周末,孩子们的爸爸带回了一个保温杯,这个保温杯的安全防护设计得很好,需要操作三个步骤...
    幼儿说阅读 3,030评论 0 1
  • 在开始解剖ES6之前,我们首先需要知道: 任何一次版本的迭代,必然是弥补以前的不足,或者为了更好发展的铺垫。而对于...
    PaytonWang阅读 1,937评论 0 3
  • 最近有一个新问题小小地困扰着我。一个男的要和我复合,但是不想继续包容我,经人提醒,他没有安全感,他是5号自保,一个...
    明慧蔡洁阅读 2,444评论 0 0