以前咱們在使用 UISearchBar 時會有修改其背景顏色的需求,使用以下方法:spa
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:frame];
searchBar.searchBarStyle = UISearchBarStyleProminent;
// --- start ---
UIView *view = searchBar.subviews[0];
view.backgroundColor = yourColor;
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
// --- end ---
[someView addSubview:searchBar];
複製代碼
此方法在 iOS13 系統下已不可以使用,會致使程序崩潰且無錯誤信息。code