ios 为什么我的SearchBar点击进入搜索状态,再次点击就会退出搜索状态呢?

发布于 2022-09-02 15:37:47 字数 3637 浏览 18 评论 0

我将searchbar放在navigationBar上,点击SearchBar会进入搜索状态(出现浮层),此时正常,但如果再次点击SearchBar,则退出搜索状态,正常应该是继续编辑才对,应该是点击取消按钮才回退出搜索状态,代码如下:

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.returnKeyType = UIReturnKeySearch;
    self.searchController.searchBar.keyboardType = UIKeyboardTypeDefault;
    self.searchController.searchBar.delegate = self;
    self.searchController.searchBar.placeholder = @"数据/文章/设计师";
    //变暗色
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    //搜索时,背景变模糊
    self.searchController.obscuresBackgroundDuringPresentation = YES;
    self.parentViewController.navigationItem.titleView = self.searchController.searchBar;
    
    
    {//设置SearchView
        
        [UIView animateWithDuration:1 animations:^{
            self.searchView = [[UIView alloc]init];
            self.searchView.frame = CGRectMake(0, 64, self.searchController.view.frame.size.width, self.searchController.view.frame.size.height-64);
            self.searchView.backgroundColor = [UIColor whiteColor];
            [self.searchController.view addSubview:self.searchView];
        }];
    
    }
    
    
    
    UILabel *cc = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    cc.text = @"激情的表演一番";
    cc.textColor = [UIColor greenColor];
    [self.searchView addSubview:cc];
    
    
    
    UISearchBar *searcher = self.searchController.searchBar;
    [searcher setContentMode:UIViewContentModeTopLeft];
    searcher.alpha = 0.6;
    //设置搜索框内的图标
    [searcher setImage:[UIImage imageNamed:@"搜索"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    //设置searchBar输入框颜色
    [[[searcher.subviews objectAtIndex:0].subviews objectAtIndex:1] setBackgroundColor:SearchBar_Color];
    self.parentViewController.navigationItem.titleView = searcher;
    //设置placeholder颜色
    [[[searcher.subviews objectAtIndex:0].subviews objectAtIndex:1] setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    // 获取输入框并修改字体颜色
    UITextField *searchField = [searcher valueForKey:@"_searchField"];
    searchField.textColor = [UIColor colorWithWhite:0.475 alpha:1.000];
    
    
    
   
    
    self.searchResltTaleview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.searchView.frame.size.width, self.searchView.frame.size.height) style:UITableViewStylePlain];
    self.searchResltTaleview.delegate = self;
    self.searchResltTaleview.dataSource = self;
    self.searchResltTaleview.tableFooterView = [[UIView alloc]init];
    [self.searchView addSubview:self.searchResltTaleview];
    
    
    //---------上述为创建SearchBar-----
    
    //下述为代理方法:
    //搜索框开始编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
    //找到searchbar下得UINavigationButton即是cancel按钮,改变他即可
    UIButton *searchBarCancelBtn = searchBar.subviews[0].subviews[2];
    [searchBarCancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [searchBarCancelBtn setTitleColor:[UIColor colorWithWhite:0.325 alpha:1.000] forState:UIControlStateNormal];
    return YES;
}


//监视搜索框的结果
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    NSString *searchText = searchController.searchBar.text;
    NSLog(@"%@", searchText);
   
   
}



    
    

然后我刚才去测试了一下,就是把SearchBar如果放在TableView的header上是没有这种问题的,而一旦放在NavigationBar上将hidesNavigationBarDuringPresentation设为NO,就会出现这种问题,请问这是什么情况

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2022-09-09 15:37:47

最终发现我只要将SearchBarController放在NavigationBar上就会出现这种状况,最终我用TextField模拟了一个SearchBar放在了Navigation的TitleView上

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文