iOS 5 で、検索バーを非表示にしない方法はありUITableViewController
ますか?
2 に答える
その場合はお勧めしません。ヘッダーの上ではなく、その上にUITableViewController
aUIViewController
が付いているUITableVIew
とうまくいきます。UISearchBar
もっと個人的な意見では、私はUITableViewController
何もお勧めしません、私はそれが実際に提供するものに対して厳しすぎると思います。何らかの理由で私がを使用していUITableViewController
て、顧客が画面に新しい要素を追加するように要求した場合、私は基本的に失敗します。
古い質問であることは知っていますが、これに対する解決策を見つけました。これは、従来の 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];
}