標籤:ios swift 搜尋方塊 uisearchbar
轉載請聲明出處:http://blog.csdn.net/jinnchang/article/details/44827923
------------------------------------------------------------------------------------------
程式碼範例
//// ViewController.swift// UISearchBarSample//// Created by jinnchang on 15/4/1.// Copyright (c) 2015年 Jinn Chang. All rights reserved.//import UIKitclass 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.prompt = "prompt" searchBar.text = "text" searchBar.barStyle = UIBarStyle.Default searchBar.searchBarStyle = UISearchBarStyle.Default searchBar.barTintColor = UIColor.orangeColor() searchBar.tintColor = UIColor.redColor() searchBar.translucent = true searchBar.showsBookmarkButton = true searchBar.showsCancelButton = true searchBar.showsSearchResultsButton = false searchBar.showsScopeBar = false 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
文章最後更新時間:2015年4月2日09:35:35。更多資料參考:
UISearchBar Class Reference
UIKit User Interface Catalog: Search Bars
論 Swift 開發入門:搜尋方塊(UISearchBar)