这个代码片段是我这周我从网上找了各种资料然后经过自己的修改终于弄好了导航的上下动画效果:
step1:==>因为这个搜索要有动画效果,所以这个页面必须要有一个导航控制器:
//1.自定义创建导航控制器
这个页面我是从其他页面跳转过来的,跳转之前我自定义了一个导航控制器:
let actionSearchTable=searchTable();
let navVC = UINavigationController(rootViewController: actionSearchTable);
navVC.navigationBar.barStyle = UIBarStyle.BlackTranslucent;
self.PResentViewController(navVC, animated: true, completion: nil);
//2.点击搜索跳转到 searchTable.swift,这个页面我继承的是 UITableViewController,而不是UiViewController,一定要注意,不然每当点击一次搜索取消的时候table上面会多出一片空白,这个原理我还不是太明白,如果你们发现了其中的原理希望能指点一二。
这个表格的数据源我引用的是一个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(); //初始化函数 override func viewDidLoad() { super.viewDidLoad(); self.title="查找商家"; initView(); } //初始化UI func initView(){ dismissBtnPrepare(); //创建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.读取表格数据 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; //获取焦点时有阴影效果 searcher.dimsBackgroundDuringPresentation=true; //获取焦点导航向上移的动画效果 searcher.hidesNavigationBarDuringPresentation=true; searchBars = searcher.searchBar tableView.tableHeaderView = searchBars searchBars?.delegate=self; searchBars?.placeholder="输入商家名称"; //取消按钮颜色和文本框光标颜色 searchBars?.tintColor=UIColor.blueWithTabbar(); //搜索框背景颜色 //searchBars?.barTintColor=UIColor.blackColor(); searcher.searchBar.sizeToFit(); self.tableView.reloadData();
//背景充满导航 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将状态栏变为白色 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); //获取点击的行索引 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) } /** 释放当前页面 */ func dismissLogin(){ self.dismissViewControllerAnimated(true, completion: nil) } func searchBarCancelButtonClicked(searchBar: UISearchBar) { print("b"); //2.3将状态栏变为白色 UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; } //搜索框开始编辑事件 func searchBarTextDidBeginEditing(searchBar: UISearchBar) { //2.3将状态栏变为白色 UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default; } func searchBarTextDidEndEditing(searchBar: UISearchBar) { //2.3将状态栏变为白色 UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; }}
//3.搜索结果页面 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]! //原数据 var filteredData:[String]! //过滤数据 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(){ //创建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(); }}
好最后效果图如下:但是其中的字符串过滤有一点点问题,解决了一定要告诉我。。
新闻热点
疑难解答