2014-02-09 49 views
0

我有UIViewController的應用程序,它包含UISegmentView,UISearchDisplayController和類似的UITableView在畫面消失。鍵盤出現,我可以做我的搜索活動。但經過我打入搜索欄第二次,它消失了 - 在第二張圖片:的UISearchBar當我點擊第二次

enter image description here

當我添加:

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

我可以看到searchResultsTableView在搜索欄:

enter image description here

有人能幫助我嗎?謝謝!

+1

這可能是一個重複:http://stackoverflow.com/questions/18989587/ios-7- uisearchdisplaycontroller-search-bar-disappears –

+0

不,它不是。但謝謝你的嘗試! –

回答

0

好吧,我終於有了解決方案,但同時我知道這不是最好的解決方案。

- (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]; 
}