2015-09-03 108 views
3

我正在使用UISearchControllerUITableView。当选中UITableView中的某个单元格时,我在整个屏幕上方添加了一个黑色的UIView,并带有一些子视图。当UISearchController处于非活动状态时,它工作得很好。但是当它活跃时,UISearchBar显示在我的黑色视图上方。如何以编程方式关闭UISearchBar?

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
self.searchController.searchResultsUpdater = self; 
self.searchController.dimsBackgroundDuringPresentation = NO; 
self.searchController.searchBar.scopeButtonTitles = [NSArray array]; 
self.searchController.searchBar.delegate = self; 
self.searchController.hidesNavigationBarDuringPresentation = NO; 
self.searchController.searchBar.backgroundImage = [self imageWithColor:[MySingleton shared].barTintColor]; 
[self.searchController.searchBar sizeToFit]; 
[self.view addSubview:self.searchController.searchBar]; 

myView = [[UIView alloc] initWithFrame:CGRectMake(0, -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; 
    myView.backgroundColor = [UIColor blackColor]; 
    myView.layer.opacity = 0.9; 
[self.view addSubview:myView]; 

我试着放置视图之前使之失去活性,

self.searchController.active = NO; 

,并添加MyView的一个导航控制器的视图

[self.navigationController.view addSubview:myView]; 

还是没有运气。任何想法?

+0

通过解雇你的意思是消除它或从它的控制权?或使其无效 –

+0

使其处于非活动状态或从中取消控制 - 只是为了使其不显示在黑色视图之上。我无法删除它,我需要在其位置上的搜索栏。 –

+0

如果你设置了会发生什么:'self.searchController.searchBar.hidden = YES;'? – dudeman

回答

0

试试这个

[self.searchController setActive:NO]; 

这是SearchController的属性按Apple文档,应该做的伎俩

+0

对不起,但我使用UISearchController,而不是弃用的UISearchDisplayController,并且该方法不适用于它。 –

+0

检查编辑和评论plz –

+0

它应该是正确的答案,但不知何故它的工作原理错误:搜索栏的文本被清除,但搜索栏保持在黑色视图之上。 –

0

有将允许你插入上面在另外一个视图中的视图的方法层次结构称为:- insertSubview:aboveSubview:。以下是如何使用它:

if (self.searchController.searchBar) 
    [self.view insertSubview:myView aboveSubview:self.searchController.searchBar]; 
else 
    [self.view addSubview:myView]; 
+0

仍然没有运气,对不起。我尝试了这一点,如果(self.searchController.active),但搜索栏仍然在视图上方。 –