【问题标题】:UISearchBar disappears when I tap second time当我第二次点击时 UISearchBar 消失
【发布时间】:2014-03-06 23:32:22
【问题描述】:

我有一个带有 UIViewController 的应用程序,其中包含 UISegmentView、UISearchDisplayController 和 UITableView,如图所示:

当我点击搜索栏时,一切都很好。键盘出现,我可以进行搜索活动。但是在我第二次点击搜索栏后,它消失了 - 在第二张图片上:

当我添加时:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(0, 20,     self.tableView.frame.size.width, self.tableView.frame.size.height);
}

我可以在 searchResultsTableView 下看到搜索栏:

有人可以帮我吗?谢谢!

【问题讨论】:

标签: ios objective-c ios7 uisearchbar uisearchdisplaycontroller


【解决方案1】:

好的,我终于有了解决方案,但同时我知道这不是最好的解决方案。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    CGRect newFrame = [self.view.superview convertRect:self.searchDisplayController.searchResultsTableView.superview.superview.frame toView:nil];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    self.searchDisplayController.searchResultsTableView.superview.superview.frame = CGRectMake(0, -40, self.tableView.frame.size.width, window.bounds.size.height - newFrame.origin.y);
    return YES;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    CGRect newFrame = [self.view.superview convertRect:self.searchDisplayController.searchResultsTableView.superview.superview.frame toView:nil];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    self.searchDisplayController.searchResultsTableView.superview.superview.frame = CGRectMake(0, 4, self.tableView.frame.size.width, window.bounds.size.height - newFrame.origin.y);

    return YES;
}

最后是内容大小:

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}



- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}

- (void) keyboardWillHide {
    UITableView *tableView = [[self searchDisplayController] searchResultsTableView];

    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多