UISearchBar 基本设置
UISearchBar 在延展中签订代理协议
@interface RootViewController () <UISearchBarDelegate>
@end
UISearchBar 创建对象
UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 100, 375, 80)];
[self.view addSubview:search];
[search release];
UISearchBar 基本属性
search.placeholder = @"search";
search.prompt = @"蓝鸥";
search.text = @"lanou";
search.barStyle = 1;
search.barTintColor = [UIColor purpleColor];
search.translucent = YES;
UISearchBar 输入设置
search.autocapitalizationType = 4;
search.keyboardType = 1;
search.showsBookmarkButton = YES;
search.showsCancelButton = YES;
search.showsSearchResultsButton = YES;
[search setShowsCancelButton:YES animated:YES];
search.showsScopeBar = YES;
search.scopeButtonTitles = @[@"0", @"1", @"2"];
search.selectedScopeButtonIndex = 2;
[search setBackgroundImage:[UIImage imageNamed:@"01.jpg"]];
[search setScopeBarBackgroundImage:[UIImage imageNamed:@"01.jpg"]];
[search setImage:[UIImage imageNamed:@"01.jpg"] forSearchBarIcon:UISearchBarIconResultsList state:(UIControlStateNormal)];
UISearchBar 代理方法 关于文本的监控
控制搜索框是否可以开始编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
return YES;
}
搜索框开始编辑时调用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
}
控制搜索框是否可以结束编辑回收键盘
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return NO;
}
输入框开始输入方法开始执行 输入框输入的内容即searchText
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSLog(@"%@", searchText);
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"%s", __func__);
}
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar{
NSLog(@"%s", __func__);
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
NSLog(@"%s", __func__);
}
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar {
NSLog(@"%s", __func__);
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
NSLog(@"%s ,%ld", __func__, selectedScope);
}