8

我有一個UISearchBar作爲子視圖添加到UICollectionView,並附加到UISearchDisplayController使用UISearchDisplayController時UICollectionView中的UISearchBar消失

我把它架在viewDidLoad

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar 
                  contentsController:self]; 
self.searchController.delegate = self; 
self.searchController.searchResultsDataSource = self; 
self.searchController.searchResultsDelegate = self; 

[self.collectionView addSubview:self.searchBar]; 

當我把另一個視圖控制器導航控制器,然後彈出它,搜索欄就會消失。只有在收集視圖滾動到足以隱藏搜索欄時纔會發生這種情況。此外,即使搜索欄消失,也可以點擊應該激活連接到其上的搜索顯示控制器的空白區域。

這隻發生在iOS 7上,如果我刪除搜索顯示控制器搜索欄不會消失。

還有一件值得一提的事情。當搜索欄消失時,如果我按另一個視圖控制器然後彈出它,欄會再次可見。

顯然這是iOS 7上的UISearchDisplayController的bug,所以有關如何解決它的任何想法?

+0

你找到一個解決辦法?我遇到同樣的問題。在collectionView標題中添加了一個UISearchDisplayController.uisearchbar。如果用戶推到另一個視圖並彈出。 uisearchbar會消失。但如果用戶點擊空白空間,它仍然可以激活。 –

+0

我找不到鍛鍊,所以最終我自己重寫了'UISearchDisplayController'。 –

+0

你的意思是你在uisearchbar上激活活動並添加你自己的uitableview?其實我只需要「主動」動畫。不確定是否有簡單的方法來產生類似的效果。 –

回答

4

我最終自己實施了UISearchDisplayController。這是我的代碼。

ZBNSearchDisplayController.h

@protocol ZBNSearchDisplayDelegate; 

@interface ZBNSearchDisplayController : NSObject<UISearchBarDelegate> 

- (id)initWithSearchBar:(UISearchBar *)searchBar contentsController:(UIViewController *)viewController; 
- (void)setActive:(BOOL)visible animated:(BOOL)animated; 

@property(nonatomic,assign) id<ZBNSearchDisplayDelegate> delegate; 
@property(nonatomic, getter = isActive) BOOL active; 
@property(nonatomic, readonly) UISearchBar *searchBar; 
@property(nonatomic, readonly) UIViewController *searchContentsController; 
@property(nonatomic, readonly) UITableView *searchResultsTableView; 
@property(nonatomic, assign) id<UITableViewDataSource> searchResultsDataSource; 
@property(nonatomic, assign) id<UITableViewDelegate> searchResultsDelegate; 

@end 

@protocol ZBNSearchDisplayDelegate <NSObject> 

@optional 

- (void)searchDisplayControllerWillBeginSearch:(ZBNSearchDisplayController *)controller; 
- (void)searchDisplayControllerDidBeginSearch:(ZBNSearchDisplayController *)controller; 
- (void)searchDisplayControllerWillEndSearch:(ZBNSearchDisplayController *)controller; 
- (void)searchDisplayControllerDidEndSearch:(ZBNSearchDisplayController *)controller; 
- (void)textDidChange:(NSString *)searchText; 
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope; 

@end 

ZBNSearchDisplayController.m

#import "ZBNSearchDisplayController.h" 

@implementation ZBNSearchDisplayController 

- (id)initWithSearchBar:(UISearchBar *)searchBar contentsController:(UIViewController *)viewController { 
    self = [super init]; 

    if (self) { 
     _searchBar = searchBar; 
     _searchBar.delegate = self; 
     _searchContentsController = viewController; 

     CGFloat y = 64.0f; 
     CGFloat height = _searchContentsController.view.frame.size.height - y; 

     _searchResultsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, y, _searchContentsController.view.frame.size.width, height)]; 
     _searchResultsTableView.scrollsToTop = NO; 
    } 

    return self; 
} 

- (void)setSearchResultsDataSource:(id<UITableViewDataSource>)searchResultsDataSource { 
    _searchResultsTableView.dataSource = searchResultsDataSource; 
} 

- (void)setSearchResultsDelegate:(id<UITableViewDelegate>)searchResultsDelegate { 
    _searchResultsTableView.delegate = searchResultsDelegate; 
} 

- (void)setActive:(BOOL)visible animated:(BOOL)animated { 
    if (!visible) { 
     [_searchBar resignFirstResponder]; 
     _searchBar.text = nil; 
     _searchBar.showsCancelButton = NO; 
    } 

    if (visible && [self.delegate respondsToSelector:@selector(searchDisplayControllerWillBeginSearch:)]) { 
     [self.delegate searchDisplayControllerWillBeginSearch:self]; 
    } else if (!visible && [self.delegate respondsToSelector:@selector(searchDisplayControllerWillEndSearch:)]) { 
     [self.delegate searchDisplayControllerWillEndSearch:self]; 
    } 

    [_searchContentsController.navigationController setNavigationBarHidden:visible animated:YES]; 

    float alpha = 0; 

    if (visible) { 
     [_searchContentsController.view addSubview:_searchResultsTableView]; 
     alpha = 1.0; 
    } 

    if ([_searchContentsController.view respondsToSelector:@selector(scrollEnabled)]) { 
     ((UIScrollView *)_searchContentsController.view).scrollEnabled = !visible; 
    } 

    if (animated) { 
     [UIView animateWithDuration:0.2 animations:^{ 
      _searchResultsTableView.alpha = alpha; 
     } completion:^(BOOL finished) { 
      self.active = visible; 
     }]; 
    } else { 
     _searchResultsTableView.alpha = alpha; 
    } 
} 

#pragma mark - UISearchBarDelegate 

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope { 
    if ([self.delegate respondsToSelector:@selector(searchBar:selectedScopeButtonIndexDidChange:)]) { 
     [self.delegate searchBar:searchBar selectedScopeButtonIndexDidChange:selectedScope]; 
    } 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    if ([self.delegate respondsToSelector:@selector(textDidChange:)]) { 
     [self.delegate textDidChange:searchText]; 
    } 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    [searchBar setShowsCancelButton:YES animated:YES]; 
    [self setActive:YES animated:YES]; 
    [_searchResultsTableView reloadData]; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [_searchResultsTableView reloadData]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    [self setActive:NO animated:YES]; 
    [self.searchResultsTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; 
} 

@end 
相關問題