在 Objective-C 的搜索栏内按下时,UISearchController 会触及安全区域

UISearchController touches in safe area when press inside the search bar in Objective-C

我有一个 tableview 并且我以编程方式创建了一个 UISearchController 并添加到表视图中。

最初它看起来不错,但当我在搜索栏内按下时,它看起来像下面。

我不想将搜索栏保留在导航栏中。而且我不能使用 UITableViewController。

找到我在下面实现的代码。

我的 .h 文件

@property (strong, nonatomic) UISearchController *searchController;  
NSMutableArray *tableData;  
NSArray *filteredContentList;

我的.m文件

// New SearchController.
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;

    [self.searchController.searchBar sizeToFit];
    self.tblUserList.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;  

#pragma mark - UISearchDisplayDelegate

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = searchController.searchBar.text;
    NSPredicate *resultPredicate;

    //resultPredicate = [NSPredicate predicateWithFormat:@"displayname contains[c] %@",searchString];
    resultPredicate = [NSPredicate predicateWithFormat:@"value contains[c] %@", searchString];
    filteredContentList = [tableData filteredArrayUsingPredicate:resultPredicate];
    //filteredContentList = [[tableData valueForKey:@"data"] filteredArrayUsingPredicate:resultPredicate];
    [self.tblUserList reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
    [self updateSearchResultsForSearchController:self.searchController];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    CGRect searchBarFrame = self.searchController.searchBar.frame;
    [self.tblUserList scrollRectToVisible:searchBarFrame animated:NO];
    return NSNotFound;
}

我想在表格视图上方显示我的搜索栏,而不是在导航栏中。

提前致谢。

我自己迟到的回答。
我使用 UITextFiled 而不是 UISearchBar。下面是我的代码实现。

[self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 

还有我的textFieldDidChange方法

-(void)textFieldDidChange:(UITextField *)textField
{
    searchTextString=textField.text;
    [self updateSearchArray:searchTextString];
}

-(void)updateSearchArray:(NSString *)searchText {
    if(searchText.length==0) {
        isFilter=NO;
    } else {
        isFilter=YES;
        filteredContentList = [[NSMutableArray alloc]init];
        NSPredicate *resultPredicate;
        resultPredicate = [NSPredicate predicateWithFormat:@"value contains[c] %@", searchText];
        filteredContentList = [tableData filteredArrayUsingPredicate:resultPredicate];
        [self.tblUserList reloadData];

    }
}  

还有我的 TableView 方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger numOfSections = 0;
    if ([filteredContentList count] || [tableData count]) {
        _tblUserList.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        numOfSections                = 1;
        _tblUserList.backgroundView = nil;
    } else {
        UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tblUserList.bounds.size.width, _tblUserList.bounds.size.height)];
         noDataLabel.text = NSLocalizedString(@"No Users found", @"Message");
         noDataLabel.textColor = [UIColor blackColor];
         noDataLabel.textAlignment = NSTextAlignmentCenter;
         _tblUserList.backgroundView = noDataLabel;
         _tblUserList.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return numOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    /*if (isSearching) {
        return [filteredContentList count];
    }
    else {
        return [tableData count];
    }*/
    if (isFilter) {
        return [filteredContentList count];
    } else {
        return [tableData count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    //cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKeyPath:@"data.displayname"];
    // Configure the cell...
    if (isFilter) {
        //cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row];
        cell.textLabel.text = [[filteredContentList objectAtIndex:indexPath.row] valueForKeyPath:@"data.displayname"];
    }
    else {
        cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKeyPath:@"data.displayname"];
    }
    return cell;
}