这个代码片段是我这周我从网上找了各种资料然后经过自己的修改终于弄好了导航的上下动画效果:

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 UIKit class searchTable: UITableViewController,UISearchBarDelegate{
lazy var dismissBtn: UIButton = { UIButton(frame: CGRectMake(, , , )) }();//返回按钮 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(, , 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 == ){ } }
/** 返回按钮 */
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 Foundation
import UIKit
class 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(, , 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 ==
{
options = options.union(.AnchoredSearch)
}
let found = s.rangeOfString(target!, options: options) return (found != nil)
}
tableData.reloadData() } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning();
} }

好最后效果图如下:但是其中的字符串过滤有一点点问题,解决了一定要告诉我。。

最新文章

  1. javascript随机打乱数组
  2. (九)WebGIS中的矢量查询(针对AGS和GeoServer)
  3. 关于CPLD与FPGA的对比分析
  4. jquery 之 Deferred 使用与实现
  5. C#获取url中参数键值对的方法
  6. NSEnumerator
  7. Windows XP与Windows 7系统常见漏洞
  8. Apple Watch: WatchKit 应用程序要点
  9. poj 2440 (找递推公式)
  10. HDU5348
  11. Windows下搭建MySQL Master Slave[转]
  12. html移动端开发注意事项
  13. ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
  14. Frame框架
  15. 今天聊一聊nuxt.js(上)
  16. 使用echarts-for-react 绘制折线图 报错:`series.type should be specified `
  17. [python] [Jupyter Notebook]
  18. javascript中的Date对象
  19. [UnityShader基础]06.#pragma multi_compile
  20. MAC环境配置

热门文章

  1. 编程之美初赛第二场AB
  2. HDU 4524
  3. RxJava资料整理
  4. 重学C++ (十一) OOP面向对象编程(2)
  5. spring与springboot中,如何在static方法里使用自动注入的属性
  6. 新手对ASP.NET MVC的疑惑
  7. JS中split使用方法和数组中元素的删除
  8. tensorflow 模型压缩
  9. 将TensorFlow模型变为pb——官方本身提供API,直接调用即可
  10. Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)