【发布时间】:2019-08-05 10:37:03
【问题描述】:
我必须维护一个相当古老且大型的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 的一些方法,但不知道如何开始。任何机构都有这方面的经验。任何信息,将不胜感激。谢谢。
我对@987654333@ 和IOS 还很陌生,我在谷歌上进行了很多搜索但没有运气。
更新:
经过几天的调试,我通过覆盖UISearchBar 类的hitTest 方法解决了我的问题。以下应该可以解决问题(也不是记录方式)
@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;
}
希望对某人有所帮助。
【问题讨论】:
标签: objective-c uisearchbar uisearchdisplaycontroller ios13