這個(gè)代碼片段是我這周我從網(wǎng)上找了各種資料然后經(jīng)過自己的修改終于弄好了導(dǎo)航的上下動(dòng)畫效果:
step1:==>因?yàn)檫@個(gè)搜索要有動(dòng)畫效果,所以這個(gè)頁(yè)面必須要有一個(gè)導(dǎo)航控制器:
//1.自定義創(chuàng)建導(dǎo)航控制器
這個(gè)頁(yè)面我是從其他頁(yè)面跳轉(zhuǎn)過來(lái)的,跳轉(zhuǎn)之前我自定義了一個(gè)導(dǎo)航控制器:
let actionSearchTable=searchTable();
let navVC = UINavigationController(rootViewController: actionSearchTable);
navVC.navigationBar.barStyle = UIBarStyle.BlackTranslucent;
self.PResentViewController(navVC, animated: true, completion: nil);
//2.點(diǎn)擊搜索跳轉(zhuǎn)到 searchTable.swift,這個(gè)頁(yè)面我繼承的是 UITableViewController,而不是UiViewController,一定要注意,不然每當(dāng)點(diǎn)擊一次搜索取消的時(shí)候table上面會(huì)多出一片空白,這個(gè)原理我還不是太明白,如果你們發(fā)現(xiàn)了其中的原理希望能指點(diǎn)一二。
這個(gè)表格的數(shù)據(jù)源我引用的是一個(gè)txt文件。格式如下圖:

//// searchResultTable.swift// 搜索框//// Created by 盧洋 on 15/11/6.// Copyright © 2015年 奈文摩爾. All rights reserved.//import UIKitclass searchTable: UITableViewController,UISearchBarDelegate{ lazy var dismissBtn: UIButton = { UIButton(frame: CGRectMake(0, 0, 24, 24)) }();//返回按鈕 var itemsString:[String]! var searcher:UISearchController! var searchBars:UISearchBar? struct SearchControllerRestorableState { var wasActive = false var wasFirstResponder = false } var restoredState = SearchControllerRestorableState(); //初始化函數(shù) override func viewDidLoad() { super.viewDidLoad(); self.title="查找商家"; initView(); } //初始化UI func initView(){ dismissBtnPrepare(); //創(chuàng)建Table self.tableView=UITableView(frame: CGRectMake(0, 80, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)); self.tableView.delegate=self; self.tableView.dataSource=self; self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cells") //1.讀取表格數(shù)據(jù) let tablePath = NSBundle.mainBundle().pathForResource("states", ofType: "txt")!; let tableData=try! NSString(contentsOfFile: tablePath, encoding: NSUTF8StringEncoding); itemsString = tableData.componentsSeparatedByString("/n") as [String]; let src = searchResultTable(data: itemsString) searcher = UISearchController(searchResultsController: src) searcher.searchResultsUpdater = src; //獲取焦點(diǎn)時(shí)有陰影效果 searcher.dimsBackgroundDuringPresentation=true; //獲取焦點(diǎn)導(dǎo)航向上移的動(dòng)畫效果 searcher.hidesNavigationBarDuringPresentation=true; searchBars = searcher.searchBar tableView.tableHeaderView = searchBars searchBars?.delegate=self; searchBars?.placeholder="輸入商家名稱"; //取消按鈕顏色和文本框光標(biāo)顏色 searchBars?.tintColor=UIColor.blueWithTabbar(); //搜索框背景顏色 //searchBars?.barTintColor=UIColor.blackColor(); searcher.searchBar.sizeToFit(); self.tableView.reloadData();
//背景充滿導(dǎo)航 definesPresentationContext = true; } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // Restore the searchController's active state. if restoredState.wasActive { searcher.active = restoredState.wasActive restoredState.wasActive = false if restoredState.wasFirstResponder { searcher.searchBar.becomeFirstResponder() restoredState.wasFirstResponder = false } } } override func viewDidDisappear(animated: Bool) { super.viewDidAppear(animated); //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIapplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; } func searchBarSearchButtonClicked(searchBar: UISearchBar) { searchBar.resignFirstResponder() } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return itemsString.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cells", forIndexPath: indexPath) cell.textLabel!.text = itemsString[indexPath.row]; return cell } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { //取消選中的樣式 tableView.deselectRowAtIndexPath(indexPath, animated: true); //獲取點(diǎn)擊的行索引 if(indexPath.row == 0){ } } /** 返回按鈕 */ func dismissBtnPrepare(){ dismissBtn.setImage(UIImage(named: "img.bundle/cancel"), forState: UIControlState.Normal) dismissBtn.addTarget(self, action: "dismissLogin", forControlEvents: UIControlEvents.TouchUpInside) self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: dismissBtn) } /** 釋放當(dāng)前頁(yè)面 */ func dismissLogin(){ self.dismissViewControllerAnimated(true, completion: nil) } func searchBarCancelButtonClicked(searchBar: UISearchBar) { print("b"); //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; } //搜索框開始編輯事件 func searchBarTextDidBeginEditing(searchBar: UISearchBar) { //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default; } func searchBarTextDidEndEditing(searchBar: UISearchBar) { //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; }}
//3.搜索結(jié)果頁(yè)面 searchResultTable.swift
//// searchResultTable2.swift// searchBarDemo//// Created by 盧洋 on 15/11/6.// Copyright © 2015年 奈文摩爾. All rights reserved.//import Foundationimport UIKitclass searchResultTable:UITableViewController,UISearchResultsUpdating{ var tableData:UITableView!; var originalData:[String]! //原數(shù)據(jù) var filteredData:[String]! //過濾數(shù)據(jù) override func viewDidLoad() { super.viewDidLoad(); self.title="輸入商家名稱"; initView() } init(data:[String]){ originalData = data super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //初始化UI func initView(){ //創(chuàng)建Table tableData=UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)); self.view.addSubview(tableData); //tableData.backgroundColor=UIColor.redColor(); tableData.delegate=self; tableData.dataSource=self; tableData.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cells") } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return filteredData.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cells", forIndexPath: indexPath) cell.textLabel!.text = originalData[indexPath.row]; return cell } func updateSearchResultsForSearchController(searchController: UISearchController) { let searchBar=searchController.searchBar; let target=searchBar.text; filteredData = originalData.filter() { s in var options = NSStringCompareOptions.CaseInsensitiveSearch if searchController.searchBar.selectedScopeButtonIndex == 0 { options = options.union(.AnchoredSearch) } let found = s.rangeOfString(target!, options: options) return (found != nil) } tableData.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning(); }}
好最后效果圖如下:但是其中的字符串過濾有一點(diǎn)點(diǎn)問題,解決了一定要告訴我。。

新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注