swift中UISearchBar的使用

前端之家收集整理的这篇文章主要介绍了swift中UISearchBar的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// 实例化
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.Default
searchbar.barTintColor = UIColor.clearColor()
        
searchbar.placeholder = "searchbar的使用"
searchbar.tintColor = UIColor.redColor()
searchbar.searchBarStyle = UISearchBarStyle.Minimal
        
// 注意:showsBookmarkButton、showsSearchResultsButton不能同时设置
searchbar.showsCancelButton = true
searchbar.showsBookmarkButton = true
// searchbar.showsSearchResultsButton = true
        
searchbar.delegate = self
        
// 键盘类型设置
searchbar.keyboardType = UIKeyboardType.EmailAddress
searchbar.returnKeyType = UIReturnKeyType.Done
searchbar.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相关文章