iOS8 UISearchController涵盖了UITableView

时间:2015-05-30 23:53:09

标签: ios uitableview swift uisearchcontroller

我为我的tableview实现了一个1UISearchController1,带有范围和一切。但是,当视图最初加载时,它无法正确显示。

根据我的行为,我看到了四种不同的东西。

这是我第一次看到视图时看到的内容。我不希望出现范围栏:

现在,如果我点击搜索栏,我会看到:

接下来,如果我再次单击“取消”按钮,我会在表格视图中看到:

最后,如果我再次点击搜索栏,我会看到第一次点击搜索栏时我的期望:

如果我再次取消,现在搜索栏会在表格视图中正确显示:

这是我的TableViewController的相关部分。

class BaseWineTableViewController: UITableViewController, UITableViewDelegate, UINavigationControllerDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {

    var resultSearchController = UISearchController()

    func ConfigureSearchController() {
        self.resultSearchController = UISearchController(searchResultsController: nil)
        self.resultSearchController.searchResultsUpdater = self
        self.resultSearchController.searchBar.delegate = self
        self.resultSearchController.delegate = self

        self.resultSearchController.searchBar.showsCancelButton = true
        self.resultSearchController.searchBar.autocapitalizationType = .None
        self.resultSearchController.searchBar.showsScopeBar = true
        self.resultSearchController.searchBar.scopeButtonTitles = ["All", CELLAR_STATUSES.Cellared, CELLAR_STATUSES.Wishlist, CELLAR_STATUSES.Finished]

        self.resultSearchController.definesPresentationContext = true
        self.resultSearchController.dimsBackgroundDuringPresentation = false
        self.tableView.tableHeaderView = resultSearchController.searchBar
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        ConfigureSearchController()
    }

2 个答案:

答案 0 :(得分:2)

我明白了。当我删除这一行时:

self.resultSearchController.searchBar.showsScopeBar = true

但是在定义范围标题的行中,它修复了它。

答案 1 :(得分:-1)

我遇到了同样的问题,似乎只出现在iOS8中。在iOS9中,搜索栏没有涵盖tableview。

我没有使用示波器栏,因此建议的修复程序不适用于我。

添加行

[self.searchController.searchBar sizeToFit];

在将searchBar分配给tableview之前,它在我的iOS8和iOS9都运行良好。

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.definesPresentationContext = YES;
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;
}
相关问题