首页 > 代码库 > 手势隐藏软键盘&设置UISearchBar
手势隐藏软键盘&设置UISearchBar
一、点击手势隐藏软键盘
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyboard:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.delegate = self;
[self.myTableView addGestureRecognizer:tapGestureRecognizer];
#pragma mark -除了cell did selected点击背景隐藏键盘,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(@"%@", [touch.view class]);
if ([@"UITableViewCellContentView" isEqualToString:NSStringFromClass([touch.viewclass])]) {
return NO;
}
return YES;
}
二、设置UISearchBar
#pragma mark- 设置UISearchBar
-(void)setSearchBarAttribute {
self.searchBar.frame = CGRectMake(0, 0, 238, 44);
[self.searchBarsetPlaceholder:NSLocalizedStringWithInternational(@"trip_custCenter_newPayment_01001",@"请输入银行名称")];
// 搜索框样式
[self.searchBar setBarStyle:UIBarStyleDefault];
// 搜索框的颜色,当设置此属性时,barStyle将失效
[self.searchBar setTintColor:[UIColor blackColor]];
// 设置是否透明
[self.searchBar setTranslucent:YES];
// 设置搜索框中文本框的文本偏移量
//[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30, 0)];
// 设置键盘样式
//[self.searchBar setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
// 是否显示取消按钮
[self.searchBar setShowsCancelButton:NO];
[self.searchBar setShowsCancelButton:NO animated:YES];
// 是否提供自动修正功能(这个方法一般都不用的)
// 设置自动检查的类型
[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];
// 是否提供自动修正功能,一般设置为UITextAutocorrectionTypeDefault
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];
// 设置代理
self.searchBar.delegate = self;
//[self.searchBar sizeToFit];
}
#pragma mark - UISearchBarDelegate 协议
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.searchBar setShowsCancelButton:NO animated:YES];
// [self.navigationController setNavigationBarHidden:YES animated:YES];
return YES;
}
// 取消按钮被按下时,执行的方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)bar{
[self.searchBar resignFirstResponder];
[self.searchBar setShowsCancelButton:NO animated:YES];
self.searchBar.text = @"";
[self selectFlightInfoBySearchText:bar.text];
}
// 键盘中,搜索按钮被按下,执行的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)bar{
[self.searchBar resignFirstResponder];
NSString *searchText = bar.text;
[self selectFlightInfoBySearchText:searchText];
// [self.navigationController setNavigationBarHidden:NO animated:YES];
}
// 当搜索内容变化时,执行该方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{
[self selectFlightInfoBySearchText:searchText];
}
#pragma mark- 模糊查询
-(void)selectFlightInfoBySearchText:(NSString *)searchText{
NSMutableArray * resultArr = [[NSMutableArray alloc] init];
if (searchText.length > 0) {
for ( PayCardObject *payCard in self.dataArray) {
NSRange searchResult=[payCard.subPayGateName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (searchResult.length>0) {
[resultArr addObject:payCard];
}
}
[tempDataArr removeAllObjects];
[tempDataArr addObjectsFromArray:resultArr];
}else{
[tempDataArr removeAllObjects];
[tempDataArr addObjectsFromArray:self.dataArray];
}
[self.myTableView reloadData];
}
手势隐藏软键盘&设置UISearchBar