ios - 当在 Objective-C 中的搜索栏内按下时,UISearchController 会触摸到安全区域

标签 ios objective-c uitableview uisearchcontroller

我有一个 tableview,我以编程方式创建了一个 UISearchController 并添加到了 tableview。 Initial Search BAR

最初看起来不错,但当我在搜索栏中按下时,它看起来如下。
When I press search bar

我不想将搜索栏保留在导航栏中。而且我无法使用 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;
}

我想在桌面 View 上方显示搜索栏,而不是在导航栏中。

提前致谢。

最佳答案

我自己迟到的回答。
我使用 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;
}  

OutPut

关于ios - 当在 Objective-C 中的搜索栏内按下时,UISearchController 会触摸到安全区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648319/

相关文章:

ios - TableViewController 不更新单元格

iphone - 如何将文件上传到webservice

ios - Swift 中的普通日期格式 "13th June 2007"

ios - 如何在我的 iPhone 应用程序中实现来自特定用户的 Instagram 照片/提要?

ios - 使搜索栏和 uisearchDisplayController 看起来像普通的输入字段

ios - UITableView 单元格文本标签颜色

ios - 将 "New"facebook connect 添加到 iPhone 应用程序

iphone - 如何在 iOS 6 中获取 Facebook 好友?

ios - 滑动删除与 iOS 10+ 类似的设计通知滑动删除 UITableViewCell

ios - swift : tableViewcell initialization