0

iOS 5 で、検索バーを非表示にしない方法はありUITableViewControllerますか?

4

2 に答える 2

7

その場合はお勧めしません。ヘッダーの上ではなく、その上にUITableViewControlleraUIViewControllerが付いているUITableVIewとうまくいきます。UISearchBarもっと個人的な意見では、私はUITableViewController何もお勧めしません、私はそれが実際に提供するものに対して厳しすぎると思います。何らかの理由で私がを使用していUITableViewControllerて、顧客が画面に新しい要素を追加するように要求した場合、私は基本的に失敗します。

于 2012-06-27T13:35:35.807 に答える
0

古い質問であることは知っていますが、これに対する解決策を見つけました。これは、従来の UITableViewController と UTSearchDisplayController で動作します。

最初に searchBar のコンテナ ビューを作成し、その中に searchbar を配置しました。コンテナーは境界にクリップしてはなりません。この後、コンテナに対する検索バーの位置を変更できます。これには、検索バーがユーザーの操作を処理しないという 1 つの問題があります。そのため、実際のフレームの下でイベントを取得する独自のコンテナーを使用する必要があります。

私たちのコンテナクラス:

@interface _SearchContainerView : UIView
@end

@implementation _SearchContainerView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.subviews.count > 0) {
        UISearchBar *searchBar = (UISearchBar *) self.subviews[0];
        CGRect f = searchBar.frame;
        f = CGRectMake(0, 0, f.size.width, f.origin.y + f.size.height);
        if (CGRectContainsPoint(f, point)) return YES;
    }
    return [super pointInside:point withEvent:event];
}
@end

プログラムで searchBar を作成する場合は、次のようなコードでこれを設定できます。

- (void)setSearchEnabled:(BOOL)searchEnabled {
    if (searchBar == nil && searchEnabled) {
        searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
        searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                                                contentsController:self];
        searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
                                     | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        searchDisplayController.delegate = self;
        searchDisplayController.searchResultsDataSource = self;

        searchContainer = [[_SearchContainerView alloc] initWithFrame:searchBar.frame];
        [container addSubview:searchBar];
        container.clipsToBounds = NO;

        self.tableView.tableHeaderView = container;

    }  else {
        [searchBar removeFromSuperview];
        self.tableView.tableHeaderView = nil;
        searchBar = nil;
        searchDisplayController = nil;
        searchContainer = nil;
    }
}

次に、tableView のスクロール位置に基づいて位置を変更できます。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (searchBar == nil || searchDisplayController.isActive) return;
    CGRect b = self.tableView.bounds;
    // Position the searchbar to the top of the tableview
    searchBar.frame = CGRectMake(0, b.origin.y, b.size.width, 44);
}

そして最後の部分は、検索後にすべてを復元することです:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    // Restore header alpha
    searchContainer.alpha = 1.0;
    // Place the searchbar back to the tableview
    [searchBar removeFromSuperview];
    [searchContainer addSubview:searchBar];
    // Refresh position and redraw
    CGPoint co = self.tableView.contentOffset;
    [self.tableView setContentOffset:CGPointZero animated:NO];
    [self.tableView setContentOffset:co animated:NO];
}
于 2015-01-05T17:05:32.303 に答える