UISearchBar 在 iOS13 崩溃的问题

在 iOS13 之前删除 UISearchBarBackground 是没问题的,代码如下:

 [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0]removeFromSuperview];

但是在 iOS13 上运行就崩溃了,崩溃信息如下:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Missing or detached view for search bar layout. The application must not remove <UISearchBarBackground: 0x7fb7c56d4c60; frame = (0 0; 269 30); userInteractionEnabled = NO; layer = <CALayer: 0x6000017d4860>> from the hierarchy.'

解决办法可以是加个判断,代码如下:

    if (@available(iOS 13.0, *)) {
        [[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0].hidden = YES;
    } else {
        [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0]removeFromSuperview];
    }

另外关于 UISearchBar 的崩溃问题,可以查看另外的网站
https://stackoverflow.com/questions/56667349/ios-13-custom-searchbar-with-uisearchbar-searchfield-not-working/58056700?r=SearchResults#58056700

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容