UISearchBar键盘没有隐藏iOS 7

时间:2014-08-02 16:48:25

标签: ios objective-c uisearchbar

我一直试图隐藏UISearchBar键盘,但我的代码无法成功运行。我有以下代码删除光标但键盘继续显示:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
     [searchBar resignFirstResponder]; // using method search bar
     [theSearchBar resignFirstResponder]; // using actual object name
     [self.view endEditing:YES];
}

我已将.h中的委托设置为<UISearchBarDelegate>并在.m

中设置searchBar.delegate = self;

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用以下方法搜索resign第一响应者:

- (UIView *)findFirstResponderBeneathView:(UIView *)view {
    for ( UIView *childView in view.subviews ) {
        if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] ) return childView;
        UIView *result = [self findFirstResponderBeneathView:childView];
        if ( result ) return result;
    }
    return nil;
}

- (void)hideKeyboard {
    [[self findFirstResponderBeneathView:self.view] resignFirstResponder];
}