在 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
网友评论