swift-教你如何实现导航上的UISearchController动画效果。
这个代码片段是我这周我从网上找了各种资料然后经过自己的修改终于弄好了导航的上下动画效果:
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();
} }
好最后效果图如下:但是其中的字符串过滤有一点点问题,解决了一定要告诉我。。

swift-教你如何实现导航上的UISearchController动画效果。的更多相关文章
- 仿知乎/途家导航栏渐变文字动画效果-b
demo.gif 效果图如上,简单分析下 1.导航栏一开始是隐藏的,随着scrollView滚动而渐变 2.导航栏左右两边的navigationItem是一直显示的 3.导航栏参考了途家app,使用了 ...
- jquer导航锚点链接动画效果和返回顶部代码
$(function(){ $(".index_nav li a").click(function(event){ //绑定按钮的单击事件 var index = this.tit ...
- [Mugeda HTML5技术教程之3] Hello World: 第一个Mugeda动画
今天我们开始我们的第一个Mugeda动画作品,并通过它来看看制作Mugeda动画的一些通用流程.在开始制作之前,请确保你已经拥有一个Mugeda网站的账号.如果还没有,你可以登录 www.mugeda ...
- 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画
[源码下载] 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 动画 示例An ...
- html小知识点汇总(浏览器导航上显示图标、div无高度时试着清除浮动、文字环绕图片、字体加粗、div按百分比分、已有的不合适的class,针对特定的标签进行修改)
1.新点击的网页,在浏览器导航上显示图标: 像这种效果: <head> <meta charset="UTF-8"> <meta name=" ...
- iOS开发——实用技术OC篇&8行代码教你搞定导航控制器全屏滑动返回效果
8行代码教你搞定导航控制器全屏滑动返回效果 前言 如果自定了导航控制器的自控制器的leftBarButtonItem,可能会引发边缘滑动pop效果的失灵,是由于 self.interactivePop ...
- 请问:关于织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表的问题
要设置织梦dedecms点击导航上的父栏目进去默认显示第一个子栏目的列表, 就按照如下图所示的方法进行操作,为什么 点击导航上的父栏目出现死循环呢, 根本浏览不了网页. 请各位大神指点指点,为什么点击 ...
- Streamr助你掌控自己的数据(3)——教你在Streamr市场上发布数据
博客说明 所有刊发内容均可转载但是需要注明出处. 教你在Streamr市场上发布数据 本系列文档主要介绍怎么通过Streamr管理自己的DATA,整个系列包括三篇教程文档,分别是:教你5分钟上传数据至 ...
- Android | 教你如何在安卓上实现通用卡证识别,一键各种卡绑定
目录 前言 通用卡证识别的应用场景 如何使用通用卡证识别服务 集成通用卡证识别服务的关键流程 开发实战 1 开发准备 1.1 在项目级gradle里添加华为maven仓 1.2 在应用级的build. ...
随机推荐
- Openfire:解决乱码问题
当部署openfire后,创建用户和发送离线消息时会出现中文字符乱码的问题.要解决这个问题需要同时配置openfire和mysql两端. 首先openfire端,在安装页面中指定odbc连接串中需要带 ...
- JAVA正則表達式小总结
近期项目中正在做后台校验,而后台校验也基本都是使用正則表達式校验.本文做一些粗略的总结. 1.字符串长度:.{1,10},注意有一个点在{}前,表示匹配全部.'{}'之前一定是一个捕获组,因此假设有其 ...
- Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)
接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...
- storm 并行度
1个worker进程运行的是1个topology的子集(注:不会出现1个worker为多个topology服务).1个worker进程会启动1个或多个executor线程来运行1个topology的c ...
- C# 操作 INI 自己工作笔记(对文本框的操作)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- uva1084
状压dp+凸包 并没有看出来凸包的性质 首先答案一定在凸包上,然后每个凸包的角加起来是一个圆,那么就相当于凸包周长加一个圆了.然后预处理,再状压dp计算即可. #include<bits/std ...
- MySQL SQL优化教程
转自:https://www.cnblogs.com/duanxz/archive/2013/02/01/2889413.html 一,查询SQL执行效率 通过show status命令了解各种SQL ...
- POJ 3264 线段树 ST
题意:给你一个数列,从中挑一段,问你这段数的最大值减最小值是多少. 思路:线段树. // by Sirius_Ren #include <cstdio> #include <algo ...
- Spring的AOP机制---- AOP的注解配置---- AOP的注解配置
3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...
- 改善用户体验 Web前端优化策略总结
前端是庞大的,包括HTML.CSS.Javascript.Image.Flash等等各种各样的资源.前端优化是复杂的,针对方方面面的资源都有不同的方式.那么,前端优化的目的是什么? 1. 从用户角度而 ...