UISearchBar现在在很多应用上面都很常见,比如当下最流行的qq,微信等。其中有很多好用的属性,下面我们来一一介绍下:
[self.searchBar setPlaceholder:@"Search"];//搜索框的占位符,就是在searchbar里面显示的一个提示文字,当你在里面输入东西的时候就不见了
[self.searchBar setPrompt:@"Prompt"];// 顶部提示文本,相当于控件的Title
[self.searchBar setBarStyle:UIBarMetricsDefault];//搜索框样式
[self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的颜色,当设置此属性时,barStyle将失效
[self.searchBar setTranslucent:YES];//设置是否透明
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"image0"]];//设置背景图片
[self.searchBarsetSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"] forState:UIControlStateNormal];//设置搜索框中文本框的背景
[self.searchBarsetSearchFieldBackgroundImage:[UIImage imageNamed:@"image0"] forState:UIControlStateHighlighted];
[self.searchBarsetSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30,30)];//设置搜索框中文本框的背景的偏移量
[self.searchBarsetSearchResultsButtonSelected:NO];//设置搜索结果按钮是否选中
[self.searchBarsetShowsSearchResultsButton:YES];//是否显示搜索结果按钮
[self.searchBarsetSearchTextPositionAdjustment:UIOffsetMake(30,0)];//设置搜索框中文本框的文本偏移量
[self.searchBar setInputAccessoryView:_btnHide];//提供一个遮盖视图
[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];//设置键盘样式
//设置搜索框下边的分栏条
[self.searchBar setShowsScopeBar:YES];//是否显示分栏条
[self.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"Singer",@"Song",@"Album", nil]];//分栏条,栏目
[self.searchBarsetScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];//分栏条的背景颜色
[self.searchBarsetSelectedScopeButtonIndex:1];//分栏条默认选中的按钮的下标
[self.searchBarsetShowsBookmarkButton:YES];//是否显示右侧的“书图标”
[self.searchBar setShowsCancelButton:YES];//是否显示取消按钮
[self.searchBar setShowsCancelButton:YESanimated:YES];
//是否提供自动修正功能(这个方法一般都不用的)
[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];//设置自动检查的类型
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];//是否提供自动修正功能,一般设置为UITextAutocorrectionTypeDefault
self.searchBar.delegate = self;//设置代理
[self.searchBar sizeToFit];
myTableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0,0, 0);
[self.viewaddSubview:myTableView];
[myTableView addSubview:self.searchBar];
下面来主要讲解一下@property(nonatomic, readwrite, retain) UIView*inputAccessoryView;属性的使用,因为在很多的搜索框中,我们都会发现,当我们点击搜索的时候,下面就会弹出一个半透明蒙板用来遮盖。
[self.searchBar setInputAccessoryView:your_View]
其实很多时候,我们并不是用这个属性来设置蒙板的,因为在它牵涉到一个背景点击取消蒙板的功能,一般我们会用一个按钮充当蒙板来做这件事。这样我们只要监听了按钮的点击,就能让蒙板退出了,不多说。直接上代码:
首先我们先定义两个属性,然后遵守UISearchBarDelegate协议。
@interface HSMessageViewController ()<UISearchBarDelegate>
@property(nonatomic,weak)UISearchBar *searchbar;
@property(nonatomic,weak)UIView *accessorybtn;
@end
接下来
UISearchBar *searchbar=[[UISearchBar alloc]init];
self.searchbar=searchbar;
UIButton *accessorybtn=[[UIButton alloc]init];
self.accessorybtn=accessorybtn;
[accessorybtn addTarget:self action:@selector(accessorybtnclick:) forControlEvents:UIControlEventTouchDragInside];
accessorybtn.frame=CGRectMake(0, 20+35, self.view.frame.size.width, self.view.frame.size.height);
accessorybtn.backgroundColor=[UIColor blackColor];
[accessorybtn setAlpha:0.0f];
[self.navigationController.view addSubview:accessorybtn];
searchbar.delegate=self;
searchbar.placeholder=@"search";
searchbar.frame=CGRectMake(0, 0, self.view.frame.size.width, 35);
self.tableView.tableHeaderView=searchbar;
对于[self.navigationController.view addSubview:accessorybtn];我解释一下,这里我的控制器是一个继承自uitableviewcontroller的控制器,同时它也被包装在一个uinavigationController的里面,因为我把accessorybtn加在导航空气里面,这样当我们点击searchbar出现蒙板时,就不能进行滚动。如果你们需要点击searchbar时也能滚动,就把accessorybtn添加到self.tableView.view上面。好了不多说了。接着上代码:
-(void)controlAccessoryView:(float)alphaValue
{
[UIView animateWithDuration:0.2 animations:^{
[self.accessorybtn setAlpha:alphaValue];
} completion:^(BOOL finished) {
if (alphaValue<=0) {
[self.searchbar resignFirstResponder];
[self.searchbar setShowsCancelButton:NO animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}];
}
-(void)accessorybtnclick:(UIButton *)accessorybtn
{
[self controlAccessoryView:0];
}
#pragma mark - searchbar的代理方法
//这里我点击了uisearchbar时候调用。因此在这里,我将取消按钮显示出来,将导航栏隐藏。并且显示设置accessorybtn按钮的透明度,来控制蒙板。下面的for循环是什么意思呢?因为相信很多人在显示取消按钮的时候,上面出现的是cancel。对于天朝人,我们怎么可能置之不理呢。因为下面的for循环就是去找到那个取消按钮,并且从新设置他显示的title。
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
[self.searchbar setShowsCancelButton:YES animated:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self controlAccessoryView:0.2];
for (id obj in [searchBar subviews]) {
if ([obj isKindOfClass:[UIView class]]) {
for (id obj2 in [obj subviews]) {
if ([obj2 isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)obj2;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}
return YES;
}
//点击了取消按钮
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self controlAccessoryView:0];
}
//点击了搜索按钮
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
}
好了 试试是不是已经可以了呢。