1. 程式人生 > >CYC-UISearchBar 點選空白回收鍵盤

CYC-UISearchBar 點選空白回收鍵盤


ios--系統發出的通知--鍵盤通知

// 監聽鍵盤將要顯示的通知 如果要顯示  那麼用keyboardWillShow來響應

// 使用時注意 當這個頁面消失的時候 立馬移除所有監聽

- (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]; // 讓tableView 下拉 交換 為 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; } // 新增Cancel: - (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;
}