UISearchBar基本用法及searchBar常用代理方法
2015-03-01 13:31
423 查看
1.初始化
UISearchBar *searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0, 320,40)];
searchBar.placeholder =@"搜索";
//和textfield一样有placeholder属性
searchBar.delegate =self;
[self.viewaddSubview:searchBar];
//由于个人需求 需要使用圆角的搜索框所以顺便对searchBar进行了一次遍历取出textfiled(ps:如果想修改searchBar背景 可以用imgaeView去接收view)
for (UIView *viewin searchBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0)
{
_textfield = [view.subviewsobjectAtIndex:1];
_textfield.layer.cornerRadius =14;
_textfield.layer.masksToBounds =YES;
}
}
2.代理方法 需要引入:<UISearchBarDelegate>
#pragma mark 输入内容就会触发
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"1");
}
#pragma mark 点击搜索栏中的textFiled触发
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
_textfield.placeholder =@"输入书名/作者";
//弹出键盘的一瞬间 给背景加了一层灰色蒙版,用于点击回首键盘。
_buttonBackground = [MyButtonmakeButtonFrame:CGRectMake(0,40, 320, 568 -40) withButtonBackGroundColor:[UIColorcolorWithWhite:0.1alpha:0.1]withButtonBackImageName:nilwithButtonTitle:nilwithButtonTitleColor:nilwithButtonTitleFont:nil];
[_buttonBackgroundaddTarget:selfaction:@selector(buttonSearch)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:_buttonBackground];
}
#pragma mark 点击search跳到搜索结果页
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
CartoonSearchViewController *vc = [[CartoonSearchViewControlleralloc]init];
[self.navigationControllerpushViewController:vc animated:YES];
}
UISearchBar *searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0, 320,40)];
searchBar.placeholder =@"搜索";
//和textfield一样有placeholder属性
searchBar.delegate =self;
[self.viewaddSubview:searchBar];
//由于个人需求 需要使用圆角的搜索框所以顺便对searchBar进行了一次遍历取出textfiled(ps:如果想修改searchBar背景 可以用imgaeView去接收view)
for (UIView *viewin searchBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0)
{
_textfield = [view.subviewsobjectAtIndex:1];
_textfield.layer.cornerRadius =14;
_textfield.layer.masksToBounds =YES;
}
}
2.代理方法 需要引入:<UISearchBarDelegate>
#pragma mark 输入内容就会触发
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"1");
}
#pragma mark 点击搜索栏中的textFiled触发
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
_textfield.placeholder =@"输入书名/作者";
//弹出键盘的一瞬间 给背景加了一层灰色蒙版,用于点击回首键盘。
_buttonBackground = [MyButtonmakeButtonFrame:CGRectMake(0,40, 320, 568 -40) withButtonBackGroundColor:[UIColorcolorWithWhite:0.1alpha:0.1]withButtonBackImageName:nilwithButtonTitle:nilwithButtonTitleColor:nilwithButtonTitleFont:nil];
[_buttonBackgroundaddTarget:selfaction:@selector(buttonSearch)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:_buttonBackground];
}
#pragma mark 点击search跳到搜索结果页
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
CartoonSearchViewController *vc = [[CartoonSearchViewControlleralloc]init];
[self.navigationControllerpushViewController:vc animated:YES];
}
相关文章推荐
- UISearchBar基本用法及searchBar常用代理方法
- UISearchbar 常用代理方法
- UISearchbar 常用代理方法
- UISearchBar基本设置 和 代理方法
- UISearchbar 常用代理方法
- UIScrollView基本用法和代理方法
- searchBar代理方法介绍
- Swift3 GCD的基本用法(二) - 队列的循环/挂起/恢复、其他常用方法
- UIScrollView基本用法和代理方法
- UIScrollView的基本使用和一些常用代理方法
- UIScrollView基本用法和代理方法
- 10.2 UIScrollview概念和用法:UIScrollView常用代理方法
- UIScrollView基本用法和代理方法
- UIScrollView 基本属性 及 常用代理方法
- UITextView的基本用法和代理方法
- UIScrollView基本用法和代理方法
- iOS--UIScrollView基本用法和代理方法
- iOS App开发中UISearchBar搜索栏组件的基本用法整理
- UISearchBar常用的属性方法
- UIScrollView基本用法和代理方法