ios--系统发出的通知--键盘通知
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:@"UIKeyboardWillHideNotification"object:nil];
[super viewDidLoad];
}
#pragma mark - 表头添加searchBar
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
SeacherView *seacherView = [[SeacherView alloc]init];
seacherView.tag =100;
seacherView.seachBar.delegate = self;
seacherView.seachBar.showsBookmarkButton = NO;
self.tableView.tableHeaderView = seacherView;
return seacherView;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:NO];
self.tableView.allowsSelection = NO;
self.tableView.scrollEnabled = NO;
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
aView.backgroundColor = [UIColor clearColor];
aView.alpha = 0.1;
[self.view addSubview:aView];
aView.tag = 10;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = @"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.tableView.allowsSelection = YES;
self.tableView.scrollEnabled = YES;
UIView *aView = [self.view viewWithTag:10];
[aView removeFromSuperview];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.tableView.scrollEnabled = YES;
self.tableView.allowsSelection = YES;
self.tableView.allowsSelection = YES;
self.tableView.scrollEnabled = YES;
UIView *aView = [self.view viewWithTag:10];
[aView removeFromSuperview];
SeacherViewController *seacherVC = [[SeacherViewController alloc] init];
NSString *nameStr = searchBar.text;
seacherVC.name = nameStr;
self.tabBarController.tabBar.hidden = YES;
[self.navigationController pushViewController:seacherVC animated:YES];
}
#pragma mark - 键盘回收
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if (touchPoint.x > 50 && touchPoint.x < kScreenWidth - 100 && touchPoint.y > 10 && touchPoint.y < 40) {
NSLog(@"x = %f, y = %f", touchPoint.x, touchPoint.y);
} else {
[self.view endEditing:YES];
}
}
#pragma mark - 通知方法执行 移除蒙版 还有 点击方法
- (void)keyboardWillHide:(NSNotification *)notification
{
UIView *aView = [self.view viewWithTag:10];
[aView removeFromSuperview];
SeacherView *seacherView = (SeacherView *)[self.view viewWithTag:100];
[seacherView.seachBar setShowsCancelButton:NO animated:YES];
[seacherView.seachBar resignFirstResponder];
self.tableView.allowsSelection = YES;
self.tableView.scrollEnabled = YES;
}