【问题标题】:scope button of UISearchBar does not work on IOS 13 when using UISearchDisplayController使用 UISearchDisplayController 时,UISearchBar 的范围按钮在 IOS 13 上不起作用
【发布时间】: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 的一些方法,但不知道如何开始。任何机构都有这方面的经验。任何信息,将不胜感激。谢谢。

我对@9​​87654333@ 和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


    【解决方案1】:

    UISearchDisplayController 已弃用,请改用 UISearchController。 看这里 https://useyourloaf.com/blog/updating-to-the-ios-8-search-controller/ 您必须迁移,因为 UISearchDisplayController 在 iOS13 中将无法正常工作。 看看这个项目,是如何管理 UISearchController 的好例子。 https://github.com/kharrison/CodeExamples/tree/master/WorldFacts 祝你好运

    【讨论】:

    • 请花 15 秒时间阅读我的整个问题,然后再发布您的答案。这种回答只会被视为垃圾邮件。谢谢。
    • 我也有大型项目,与您遇到的问题完全相同。我为您提供的这些示例帮助我解决了问题,现在范围按钮可以正常工作。我花了1天的时间把所有东西放在一起。当您提出问题时,请先进行一些研究。
    • 感谢您的帮助。当然,我在这里提出问题之前做了很多搜索。我不知道您的项目有多大和多旧,但就我而言,它无法在 1 或 2 个工作日内完成。这不仅仅是查找和替换东西。它需要的远不止你给出的例子(尽管有成千上万的例子和教程比你给出的关于 UISearchController 的更容易理解)。不仅是范围按钮,您还必须更改取消按钮的操作,当用户在框外触摸时处理,数据填充方式......并向您的客户解释......
    • 我的旧项目(在调试器下编译和运行良好)因启动时崩溃而被 Apple 拒绝。符号化的崩溃日志(由应用审查团队提供)显示这是由于其中一个视图中存在 UISearchDisplayController。因此,如果您的应用是通过 App Store 分发的,您 > 必须
    • 没有。有时,苹果审查团队的工作方式不可预测。有很多帖子显示同样的问题。应用程序在接受审查时崩溃,但在下一次审查时通过而不更改源代码(只是重建)。上面的技巧在 Xcode10 上工作得很好,给了我半年的时间来升级到 Xcode11。不幸的是,Xcode11 中不再存在 UISearchDisplayController 并导致 IOS13.3 崩溃,所以我决定不使用 UISearchDisplayController,因为 UISearchController 无法为我提供所需的 UI 和动画。 (我 11 年的项目仍在使用 NSURLConnection)
    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2019-11-02
    相关资源
    最近更新 更多