tableHeaderView UISearchBar没有显示

时间:2014-05-29 21:24:44

标签: ios uitableview uisearchbar

我已经以编程方式向我的UITableView添加了一个UISearchBar,它显示得很好,直到我决定在我的UITableView中添加一个偏移量来隐藏加载视图时的UISearchBar。我想帮助再次显示它。

这就是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.contentOffset = CGPointMake(0.0f, 44.0f);

    mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
    mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    mySearchBar.keyboardType = UIKeyboardTypeAlphabet;
    mySearchBar.delegate = self;
    self.tableView.tableHeaderView = mySearchBar;

    // Create the search display controller
    UISearchDisplayController  *searchController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
    searchController.searchResultsDataSource = self;
    searchController.searchResultsDelegate = self;

我不确定从哪里开始。

1 个答案:

答案 0 :(得分:2)

此代码适用于iOS6和iOS7。 请注意,在iOS7中,您将失去NavigationBar的透明度

if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}
[self.tableView setContentOffset:CGPointMake(0, mySearchBar.frame.size.height)];

如果要在iOS7中保存默认透明度,请使用以下代码:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {

        [self.tableView setContentOffset:CGPointMake(0, -20)];
    }
    else {
        [self.tableView setContentOffset:CGPointMake(0, mySearchBar.frame.size.height)];
    }
}