8

UICollectionViewにサブビューとして追加されたUISearchBarがあり、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でのみ発生し、検索ディスプレイコントローラを削除しても検索バーは消えません。

もう1つ言及する価値があります。検索バーが消えたときに、別のView Controllerをプッシュしてポップすると、バーが再び表示されます。

明らかに、これはiOS 7のUISearchDisplayControllerのバグですので、回避する方法はありますか?

+0

あなたは解決策を見つけますか?私は同じ問題に遭遇している。 UISearchDisplayController.uisearchbarをcollectionViewヘッダーに追加しました。ユーザーが別のビューを押してポップバックした場合。 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 
関連する問題