关于ios:UISearchController搜索栏出现但无法正常工作/触发

UISearchController search bar appears but not working/triggering

在我的视图控制器中,我以编程方式添加了一个搜索栏,它的显示方式完全与我想要的方式相同,但是当我单击它时,什么也没做,我无法在搜索栏上写任何东西/键入任何内容,并且没有错误日志。我以相同的方式在另一个视图控制器中添加了搜索栏,它确实可以工作,但特别是它不起作用。我正在从以前的视图控制器推这个视图控制器,因此它在左侧也有一个向后导航按钮(检查图像)。

在视图控制器中有一个TableView和一个CollectionView,我想添加对表视图的搜索。我想知道是否将这两者都放在同一个视图控制器中有什么关系。

添加搜索栏的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)viewDidLoad {
   //other stuff on view controller
   [self setSearchView];
}

-(void)setSearchView{
    //searchController is added as a property on .h file
    //and all the delegate are also added
    //@property (strong, nonatomic) UISearchController *searchController;

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater= self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.navigationController.toolbarHidden=YES;

    self.navigationItem.titleView=self.searchController.searchBar;

    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.definesPresentationContext = YES;
}

我要从以前的视图控制器进入该视图控制器,并在单击按钮时推动该视图控制器。我正在使用nib文件
按钮动作

1
2
3
4
5
6
7
8
- (void)ButtonClicked {
   MYNextViewController *nextViewController = [[MYNextViewController alloc] initWithNibName:@"MYNibFileName" bundle:nil];

    // I also have a tab bar on the bottom which i am hiding for that view controller when pushing
    nextViewController .hidesBottomBarWhenPushed=YES;

    [self.navigationController pushViewController:nextViewController  animated:YES];
}

图片:
enter


最后,由于有了@ Tj3n,我能够通过视图层次的一些更改来解决此问题,但是不确定这是否是正确的解决方案,但现在可以正常工作了..
我还必须删除此行,并为其他目的添加更多代码

1
self.definesPresentationContext = YES; //removed...

该问题的答案也很有帮助


我遇到了类似的问题,并在这里尝试了答案,但是它对我没有用。然后偶然发现了这个帖子https://forums.developer.apple.com/thread/122972

基本上,对于我来说,禁用"自动同步粘贴板"是可行的。 (模拟器->编辑->自动同步粘贴板)


对于您的特定情况,请使用此

1
 self.searchController.obscuresBackgroundDuringPresentation = NO;

这将允许您使用同一表视图与搜索结果进行交互。


//请使用此代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.navigationController.navigationBar.bounds.size.height)];
_searchBar.tintColor = [UIColor blueColor];

//[_searchBar setImage:[UIImage imageNamed:@"cross_icon"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
_searchBar.placeholder = @"Enter drug name";
//_searchBar.showsBookmarkButton = YES;
_searchBar.barTintColor = [UIColor clearColor];

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor whiteColor];
txfSearchField.tintColor = [UIColor blackColor];
txfSearchField.textColor = [UIColor blackColor];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
_searchBar.showsCancelButton = NO;
[_searchBar becomeFirstResponder];
[self.navigationController.navigationBar addSubview:_searchBar];