【问题标题】:UISearchBar in a UICollectionView disappears when using UISearchDisplayController使用 UISearchDisplayController 时 UICollectionView 中的 UISearchBar 消失
【发布时间】:2014-01-22 22:35:03
【问题描述】:

我将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 的一个错误,那么有什么解决方法的想法吗?

【问题讨论】:

  • 您找到解决方案了吗?我遇到了同样的问题。向 collectionView 标头添加了 UISearchDisplayController.uisearchbar。如果用户推送到另一个视图并弹回。 uisearchbar 将消失。但是如果用户点击空白区域,它仍然可以激活。
  • 我找不到锻炼,所以我最终自己重写了UISearchDisplayController
  • 你的意思是你在 uisearchbar 上为活动设置动画并自己添加 uitableview ?实际上我只需要“活动”动画。不确定是否有一种简单的方法可以产生类似的效果。
  • 是的,我管理导航栏/搜索栏动画并添加/删除 UITableView。我将在单独的答案中发布代码。

标签: ios ios7 uicollectionview uisearchbar uisearchresultscontroller


【解决方案1】:

我最终自己实现了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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多