iOS-UISearchBar 键盘关闭的坑

问题:
在导航栏中添加UISearchBar 键盘关闭不是很友好,需要关闭的时候不太好操作

解决办法:
开始编辑UISearchBar的时候,在window上加一层透明view,view加一个触摸事件,触摸的时候将键盘关闭 并把这层view移除

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
   UIWindow *window=[UIApplication sharedApplication].keyWindow;
   self.keyview = [[FWBaseView alloc]initWithFrame:window.frame];
   self.keyview.backgroundColor = [UIColor clearColor];
   WeakSelf;
   self.keyview.clickAction = ^{
       if ([weakSelf.searchBar isFirstResponder]) {
           [weakSelf.searchBar resignFirstResponder];
       }
       [weakSelf.keyview removeFromSuperview];
   };
   [window addSubview:self.keyview];
}

点击搜索的时候也需要将这个view移除

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if ([searchBar.text isNotBlank]) {
        if ([searchBar isFirstResponder]) {
            WeakSelf;
            if (self.keyview) {
                [weakSelf.keyview removeFromSuperview];
            }
            [searchBar resignFirstResponder];
        }
    }
}

你可能感兴趣的:(iOS-UISearchBar 键盘关闭的坑)