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 > 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

你可能感兴趣的:(UISearchBar 在 iOS13 崩溃的问题)