ios – UICollectionView中的UISearchBar在使用UISearchDisplayController时消失

我有一个UISearchBar作为子视图添加到UICollectionView,并附加到UISearchdisplayController.

我在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,所以关于如何解决它的任何想法?

解决方法

我最终自己实现了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,1,1) animated:NO];
}

@end

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...