通常UISearchbar都需要去除其背景色来与自己的界面风格保持协调,但是UISearchbar的设计随着iOS版本的升级不断地在发生着变化。在iOS7.0之前,UISearchbar视图里直接包含UISearchBarBackground和UISearchBarTextField两个视图,在iOS7.0及之后,UISearchbar视图里包含的是一个UIView视图,UIView视图里才是UISearchBarBackground和UISearchBarTextField两个视图。经多次试验,发现去除UISearchbar视图里的UISearchBarBackground后UISearchbar的背景就透明了
以下为处理代码
for (UIView *view in self.searchBar.subviews) {
// for before iOS7.0if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[view removeFromSuperview];
break;
}
// for later iOS7.0(include)
if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
[[view.subviews objectAtIndex:0] removeFromSuperview];
break;
}
}