swift中UISearchBar的使用

swift中UISearchBar的使用,第1张

概述// 实例化let searchbar = UISearchBar(frame: CGRectMake(10.0, 10.0, (CGRectGetWidth(self.view.frame) - 10.0 * 2), 40.0))self.view.addSubview(searchbar) searchbar.backgroundColor = UIColor.yello
// 实例化let searchbar = UISearchbar(frame: CGRectMake(10.0,10.0,(CGRectGetWIDth(self.vIEw.frame) - 10.0 * 2),40.0))self.vIEw.addSubvIEw(searchbar)        searchbar.backgroundcolor = UIcolor.yellowcolor()        searchbar.barStyle = UIbarStyle.Defaultsearchbar.barTintcolor = UIcolor.clearcolor()        searchbar.placeholder = "searchbar的使用"searchbar.tintcolor = UIcolor.redcolor()searchbar.searchbarStyle = UISearchbarStyle.Minimal        // 注意:showsBookmarkbutton、showsSearchResultsbutton不能同时设置searchbar.showsCancelbutton = truesearchbar.showsBookmarkbutton = true// searchbar.showsSearchResultsbutton = true        searchbar.delegate = self        // 键盘类型设置searchbar.keyboardType = UIKeyboardType.EmailAddresssearchbar.returnKeyType = UIReturnKeyType.Donesearchbar.secureTextEntry = false        // 输入源设置(与textfIEle、或textvIEw类似)// searchbar.inputAccessoryVIEw = nil// searchbar.inputVIEw = nil                // 第一响应,即进入编辑状态searchbar.becomeFirstResponder()// 放弃第一响应,即结束编辑// searchbar.resignFirstResponder()// searchbar.endEditing(true) // 结束编辑
// 添加协议class VIEwController: UIVIEwController,UISearchbarDelegate {    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()      ....     }}
// 实现代理方法// MARK: - UISearchbarDelegate    func searchbarShouldBeginEditing(searchbar: UISearchbar) -> Bool  {        print("1 searchbarShouldBeginEditing")        return true}    func searchbarTextDIDBeginEditing(searchbar: UISearchbar) {        print("2 searchbarTextDIDBeginEditing")}    func searchbar(searchbar: UISearchbar,textDIDChange searchText: String) {        print("3 searchbar")                print("3 text=\(searchbar.text),string=\(searchText)")}    func searchbar(searchbar: UISearchbar,shouldChangeTextInRange range: NSRange,replacementText text: String) -> Bool {        print("4 searchbar")                print("4 text=\(searchbar.text),range=\(range),string=\(text)")                return true}    func searchbarShouldEndEditing(searchbar: UISearchbar) -> Bool{        print("5 searchbarShouldEndEditing")          return true}    func searchbarTextDIDEndEditing(searchbar: UISearchbar) {        print("6 searchbarTextDIDEndEditing")}    func searchbarSearchbuttonClicked(searchbar: UISearchbar) {        print("7 searchbarSearchbuttonClicked")                searchbar.endEditing(true)}    func searchbarBookmarkbuttonClicked(searchbar: UISearchbar) {        print("8 searchbarBookmarkbuttonClicked")                searchbar.endEditing(true)}    func searchbarCancelbuttonClicked(searchbar: UISearchbar) {        print("9 searchbarCancelbuttonClicked")                searchbar.endEditing(true)}    func searchbarResultsListbuttonClicked(searchbar: UISearchbar) {        print("10 searchbarResultsListbuttonClicked")                searchbar.endEditing(true)}    func searchbar(searchbar: UISearchbar,selectedScopebuttonIndexDIDChange selectedScope: Int) {        print("11 searchbar")}


总结

以上是内存溢出为你收集整理的swift中UISearchBar的使用全部内容,希望文章能够帮你解决swift中UISearchBar的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071487.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存