UISearchBar 背景图片去除

//去掉背景图片
        for(UIView* view in [_searchBar.subviews firstObject].subviews) {
             
            UIView *subview = view;
            if(@available(iOS 13.0, *)) {

                if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){
                    //去掉背景颜色
                    [((UIImageView*)view) setImage:nil];
                }
                if ([view isKindOfClass:NSClassFromString(@"_UISearchBarSearchContainerView")]){
                    //ios13输入文本框又往里移了一层
                    subview = [view.subviews firstObject];
                }

            }

            if([subview isKindOfClass:[UITextField class]]) {

                UITextField* searchField = (UITextField*)subview;
                // 删除searchBar输入框的背景
                [searchField setBackground:nil];
                [searchField setBorderStyle:UITextBorderStyleNone];
                //设置文本框的背景颜色
                searchField.backgroundColor = [UIColor clearColor];
                //searchField.font = xxx;
                // 设置圆角
//                searchField.layer.cornerRadius = 3;
//                searchField.layer.masksToBounds = YES;
                break;

            }

        }

推荐阅读更多精彩内容