我必须维护一个相当老的大型Objective-C项目。在iOS12
之前,一切正常,但从iOS13
开始,UISearchBar
的范围按钮栏无法与UISearchDisplayController
一起使用。
我知道UISearchDisplayController
已过时,但由于某些原因,我真的不想将其迁移到UIDisplayController
。我进行了一些测试,发现可能与UIDisplayController
有关的问题,因为它阻止了用户触摸范围按钮。
我的代码如下:
@implementation MyUISearchDisplayController
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if(self.active == visible){ return; }
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible)
{
[self.searchBar becomeFirstResponder];
}
else
{
[self.searchBar resignFirstResponder];
}
}
@end
@interface TopBarViewController ()
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) UISearchDisplayController *searchController;
@end
@implementation TopBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
//The default state for show-cancel-button and show-scope-bar of searchBar is false (not showing)
self.searchBar.scopeButtonTitles = @[@"scope1", @"scope2", @"scope3", @"scope4"];
self.searchController = [[MyUISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
}
上面的代码在IOS12
之前同样有效,但是从IOS13
开始,我无法触摸示波器按钮。我想与UIDisplayController
类有关的问题。因此,我正在考虑覆盖UIDisplayController
的某些方法,但不知道如何开始。任何人都有经验。任何信息,将不胜感激。谢谢。
我对Objective-C
和IOS
相当陌生,我在Google上进行了大量搜索,但没有运气。
更新:经过几天的调试,我通过覆盖hitTest
类的UISearchBar
方法解决了我的问题。以下应该可以解决问题(这也是没有记录的方法)
@implementation MyUISearchBar
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView * touchedView = [super hitTest:point withEvent:event];
if(check version stuff){
return touchedView;
}
UIView * scopeBar = [self performSelector:NSSelectorFromString(@"_scopeBar")];
if(user touched the scopeBar){
return scopeBar;
}
return touchedView;
}
希望对您有所帮助。
UISearchDisplayController已过时,请改用UISearchController。看这里https://useyourloaf.com/blog/updating-to-the-ios-8-search-controller/您必须迁移,因为UISearchDisplayController在iOS13中无法正常工作。看这个项目,是如何管理UISearchController的好例子。https://github.com/kharrison/CodeExamples/tree/master/WorldFacts祝你好运