------------------------------------------------------------------------------------------
// // ViewController.swift // UISearchBarSample // // Created by jinnchang on 15/4/2. // Copyright (c) 2015年 Jinn Chang. All rights reserved. // import UIKit class ViewController: UIViewController,UISearchBarDelegate { var searchBar: UISearchBar! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. searchBar = UISearchBar(frame: CGRectMake(0,20,self.view.frame.size.width,80)) searchBar.placeholder = "search" searchBar.barStyle = .Default searchBar.showsBookmarkButton = true searchBar.showsCancelButton = true searchBar.delegate = self self.view.addSubview(searchBar) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // 每次输入内容改变触发事件 func searchBar(searchBar: UISearchBar,textDidChange searchText: String) { println("筛选内容:\(searchText)") } // 书签按钮触发事件 func searchBarBookmarkButtonClicked(searchBar: UISearchBar) { println("搜索历史记录") } // 取消按钮触发事件 func searchBarCancelButtonClicked(searchBar: UISearchBar) { println("取消搜索") } // 搜索触发事件 func searchBarSearchButtonClicked(searchBar: UISearchBar) { println("开始搜索") } }------------------------------------------------------------------------------------------
GitHub 上项目地址:UISearchBarSample
控件更多相关属性及方法参考:UISearchBar Class Reference 文章最后更新时间:2015年4月3日11:42:05