【发布时间】:2018-05-02 20:16:31
【问题描述】:
我有以下代码来设置我的UISearchController
- (void)viewDidLoad {
[super viewDidLoad];
// Arrays init & sorting.
self.cities = [@[@"Boston", @"New York", @"Oregon", @"Tampa", @"Los Angeles", @"Dallas", @"Miami", @"Olympia", @"Montgomery", @"Washington", @"Orlando", @"Detroit"] sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
return [obj2 localizedCaseInsensitiveCompare:obj1] == NSOrderedAscending;
}];
self.results = [[NSMutableArray alloc] init];
// A table view for results.
UITableView *searchResultsTableView = [[UITableView alloc] initWithFrame:self.tableView.frame];
searchResultsTableView.dataSource = self;
searchResultsTableView.delegate = self;
// Registration of reuse identifiers.
[searchResultsTableView registerClass:UITableViewCell.class forCellReuseIdentifier:Identifier];
[self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:Identifier];
// Init a search results UITableViewController and setting its UITableView.
self.searchResultsTableViewController = [[UITableViewController alloc] init];
self.searchResultsTableViewController.tableView = searchResultsTableView;
// Init a UISearchController with its UITableViewController for results.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsTableViewController];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
// Make an appropriate size for UISearchBar and add it as a header view for initial UITableView.
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
// Enable presentation context.
self.definesPresentationContext = YES;
}
这很好用,除了在 iOS 11 上。似乎有两个问题。首先是当我搜索时,表格栏和搜索栏之间存在间隙。
另一个问题是当我退出UISearchController 时,tableView 中的第一个单元格被搜索栏略微截断。
有没有办法解决这个问题?
【问题讨论】:
-
您的第一个问题将通过以下方式解决:self.automaticallyAdjustsScrollViewInsets = NO;
-
对我不起作用。
标签: ios objective-c uitableview uisearchcontroller searchbar