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

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

我将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,就会出现这种问题,请问这是什么情况


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

【热门文章】
【热门文章】