Ios Can';t在navigationItem标题视图中编辑UISearchController UISearchBar
在navigationItem标题视图中设置UISearchController搜索栏时,无法编辑该搜索栏 在我的viewDidLoad中,我正在配置UISearchControllerIos Can';t在navigationItem标题视图中编辑UISearchController UISearchBar,ios,objective-c,cocoa-touch,uisearchcontroller,Ios,Objective C,Cocoa Touch,Uisearchcontroller,在navigationItem标题视图中设置UISearchController搜索栏时,无法编辑该搜索栏 在我的viewDidLoad中,我正在配置UISearchController self.searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([SearchViewController class])]; self.searchViewContro
self.searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([SearchViewController class])];
self.searchViewController.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchViewController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.navigationItem.titleView = self.searchController.searchBar;
我无法点击搜索栏。光标不会出现,并且没有用户交互
奇怪的是,如果我在本地初始化UISearchController而不将其设置为属性,那么我可以编辑搜索栏,只是没有委托回调
self.searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([SearchViewController class])];
self.searchViewController.delegate = self;
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchViewController];
searchController.delegate = self;
searchController.searchResultsUpdater = self;
searchController.searchBar.delegate = self;
self.navigationItem.titleView = searchController.searchBar;
另一个有趣的行为是清除按钮起作用(如果在初始化时搜索栏中设置了一些文本)。在第一个代码块中,您正在使用
SearchViewController
标识符进行实例化。在第二个示例中,您使用的是HBSearchViewController
。这表明,除了使用/不使用插座外,您的代码可能还有另一个不同之处。将搜索栏设置为导航标题视图:
self.navigationItem.titleView = self.searchBarTop;
然后只需将此视图设置为导航栏的左/右按钮
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
我希望这对你有用 我正在设置
self.definesPresentationContext=YES显示相关视图控制器的视图控制器中的代码>
这必须设置为self.definesPresentationContext=NO视图中的code>将出现:
现在,显示的视图控制器中的搜索栏可以编辑。我刚刚在我的应用程序中解决了一个非常类似的问题,我想我会分享解决方案,以防现有解决方案无法为您解决
在我的例子中,我有一个选项卡栏应用程序,在选项卡中有我的自定义控制器,嵌入在导航控制器中。症状完全相同,搜索栏显示在导航栏的标题区域,但不是交互式的
我发现问题在于我使用了UITabBarController的自定义子类,并且覆盖了viewWillAspect(动画:)
方法,但忘了在实现中调用super.viewWillAspect(动画:)
。另外一个症状是,当我切换选项卡时,搜索栏突然变得交互,一切正常,只是初始选项卡上的交互被禁用
我希望这对某人有所帮助。我也有同样的问题
假设您有FirstViewController和SecondViewController,booth在标题视图上有一个UISearchBar
为了解决这个问题,我在booth UIViewController上输入了这个代码
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.definesPresentationContext = true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.definesPresentationContext = false
}
对不起,那是我的错。我在第一个代码块中删除了前缀,在第二个代码块中忘记了。我有同样的问题,建议的答案都不起作用。你找到解决办法了吗?@SilviaHisham是的,对我有效的是我下面的答案set self.definesPresentationContext=NO;到底在哪个视图控制器中?是有UISearchController还是在它之前?@SilviaHisham您可以查看我的答案以获得澄清谢谢!你的答案是唯一对我有用的答案。这个!这是唯一有效的答案!非常感谢。