- (UISearchController *)searchController{
if (!_searchController) {
_searchController = [[UISearchController alloc]initWithSearchResultsController:self.resultVC];
self.definesPresentationContext = YES;
_searchController.dimsBackgroundDuringPresentation = YES;
_searchController.hidesNavigationBarDuringPresentation = YES;
_searchController.searchBar.barTintColor = [UIColor yellowColor];
// 可以通过此种方式可以拿到搜索框,修改搜索框的样式
UITextField *searchField = [_searchController.searchBar valueForKey:@"_searchField"];
searchField.backgroundColor = [UIColor redColor];
searchField.leftView = nil;
searchField.layer.cornerRadius = 4;
searchField.clipsToBounds = YES;
searchField.leftViewMode = UITextFieldViewModeNever;
UIView *backgroundview = searchField.subviews.firstObject;
backgroundview.backgroundColor = [UIColor redColor];
NSInteger count = backgroundview.subviews;
for (UIView *sub in backgroundview.subviews) {
NSLog(@"%@", NSStringFromClass(sub.class));
}
}
return _searchController;
}
以上是设置代码 ,
UI 如下
查看层级,发现有个黑色图层
尝试删除黑色view保留红色view,却不成功,
从层级来看,黑色是红色的subView, 但是 代码:
NSInteger count = backgroundview.subviews;
for (UIView *sub in backgroundview.subviews) {
NSLog(@"%@", NSStringFromClass(sub.class));// 不会打印
}
该如何删除这个图层