如何禁UISearchController中uisearchbar点击上移获取焦点后的往上移动的动画

关于UISearchController偏移问题 - 跟谁学
搜索你想学的科目、老师试试,例如“数学”搜索吉安
&&关于UISearchController偏移问题serachBar的位置改变不了吗? 我知道是多了我那两个按钮的高度,但是怎么能让他上去呢? (void)updateSearchResultsForSearchController:(UISearchController *)searchController{ [self.kwSearchController.searchBar setFrame:CGRectMake(0, 0, KScreenWidth, 44)]; } 我是加在tableView的HeaderView上的 在这个代理里修改位置也不好使MichealRGB袁dada
请问题主解决了吗?
首先:讲道理,你这个 TabBar 不应该是加在 headerView 上,因为 TabBar 应该是固定位置吧。其次:做这个界面,你最好分开处理视图,不使用 UISearchController,而是去使用自定义的 TabBar + UISearchBar + UITableView
相关问题大家都在看最新提问
关注我们官方微信关于跟谁学服务支持帮助中心swift-教你怎么实现导航上的UISearchController动画效果 - Swift当前位置:& &&&swift-教你怎么实现导航上的UISearchController动画swift-教你怎么实现导航上的UISearchController动画效果&&网友分享于:&&浏览:0次swift-教你如何实现导航上的UISearchController动画效果。&  这个代码片段是我这周我从网上找了各种资料然后经过自己的修改终于弄好了导航的上下动画效果:
step1:==&因为这个搜索要有动画效果,所以这个页面必须要有一个导航控制器:
//1.自定义创建导航控制器
这个页面我是从其他页面跳转过来的,跳转之前我自定义了一个导航控制器:
   let actionSearchTable=searchTable();
& & & & let navVC = UINavigationController(rootViewController: actionSearchTable);
& & & & navVC.navigationBar.barStyle = UIBarStyle.BlackT
& & & & 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(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.tableView.dataSource=
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 = ponentsSeparatedByString("\n") as [String];
let src = searchResultTable(data: itemsString)
searcher = UISearchController(searchResultsController: src)
searcher.searchResultsUpdater =
//获取焦点时有阴影效果
searcher.dimsBackgroundDuringPresentation=true;
//获取焦点导航向上移的动画效果
searcher.hidesNavigationBarDuringPresentation=true;
searchBars = searcher.searchBar
tableView.tableHeaderView = searchBars
searchBars?.delegate=
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.LightC
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.LightC
//搜索框开始编辑事件
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
//2.3将状态栏变为白色
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.D
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
//2.3将状态栏变为白色
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightC
//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(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height));
self.view.addSubview(tableData);
//tableData.backgroundColor=UIColor.redColor();
tableData.delegate=
tableData.dataSource=
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.searchB
let target=searchBar.
filteredData = originalData.filter()
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();
好最后效果图如下:但是其中的字符串过滤有一点点问题,解决了一定要告诉我。。
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有iOS---搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
更新日期:
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式.
添加UISearchController属性:
@property(strong, nonatomic) UISearchController *searchC
@property(strong, nonatomic) NSMutableArray *allC
UISearchController初始化
在viewDidLoad中初始化UISearchController:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]
UISearchResultsUpdating协议
使用UISearchController要继承UISearchResultsUpdating协议, 实现其中的UISearchResultsUpdating方法.
- searchController delegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.filteredCities removeAllObjects];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", self.searchController.searchBar.text];
self.filteredCities = [[self.allCities filteredArrayUsingPredicate:searchPredicate] mutableCopy];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
UISearchController的searchBar中的内容一旦发生变化, 就会调用该方法. 在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.
更新UITableView
引入UISearchController之后, UITableView的内容也要做相应地变动: 即cell中要呈现的内容是allCities, 还是filteredCities.&这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.&UITableView相关的很多方法都要根据active来做判断:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (!self.searchController.active) {
return self.cityKeys.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!self.searchController.active) {
NSString *key = self.cityKeys[section];
NSArray *citySection = self.cityDict[key];
return citySection.
return self.filteredCities.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleN
UISearchController的移除
在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.searchController.active) {
self.searchController.active = NO;
[self.searchController.searchBar removeFromSuperview];
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
程序员的目标是
征服星辰的大海. */ ------问题再现:
公司的项目里很多用到了好友管理,因此搜索框的使用是不可避免的.
我在之前搭配SearchBar和DisplayController使用的时候时常会出现一些问题,比如搜索框的位置的变化,整个屏幕的偏移等等,后来改成简单的使用Sear ...
在这篇随笔里,我们只要知道UIWebView是什么就可以了. UIWebView 是苹果提供的用来展示网页的UI控件,它也是最占内存的控件. iOS8.0之后出现了webkit框架,WKWebView相比UIWebView节省了1/4~1/3的内存,速度快,但是没缓存功能. 对于一些购物类app网页的展示是必不可免的,因此UIWebView对于我们来说也是应 ...
一,效果图. 二,工程图. 三,代码. RootViewController.h #import &UIKit/UIKit.h& @interface RootViewController : UIViewController //方便跳转到相应的页面 @property (nonatomic , retain) NSString*
工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. &iOS 自定义控件开发(上)& &iOS 自定义控件开发(中)& #0 目标 希望可以写一个通用的图表控件(仅针对此项目),虽然开发难度增大,但是可以学习到很多知识.并且控件使用简单,可以自适应大小,支持屏幕旋转. #1 准备工作 网 ...
系统:OS X EI Capitan 版本:10.11.2 开发工具:XCode:7.2 要使用CocoaPods,那么就需要先安装哦,你安装了么?如果没安装那就请阅读我的前篇&OS X 10.11 CocoaPods的安装(图文并茂)&. 如果安装好了,那么就接着阅读这篇博文吧. 1.新建一个Xcode工程,然后进入工程根目录: 2.然后如果要 ...

我要回帖

更多关于 uisearchbar点击上移 的文章

 

随机推荐