UISearchBar样式修改

-(UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(8, 27, HOME_SCREEN_WIDTH - 64.0, 30.0)];
        _searchBar.barStyle = UIBarStyleDefault;
        _searchBar.placeholder = @"ID";
        [self setSearchBarBgColor:_searchBar];
        _searchBar.delegate = self;
        _searchBar.tintColor = [UIColor blackColor];//光标颜色
        //_searchBar.barTintColor = [UIColor redColor];//背景颜色
     
        [_searchBar setImage:[UIImage imageNamed:@"关闭搜索"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
         [_searchBar setImage:[UIImage imageNamed:@"关闭搜索按下"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];//清除按钮
    //    [_searchBar setImage:[UIImage imageNamed:@"搜索按钮"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];//搜索按钮
        
        
        UITextField *txfSearchField = [_searchBar valueForKey:@"searchField"];
        txfSearchField.backgroundColor = white_color;// RGB(206, 205, 210);
        txfSearchField.layer.cornerRadius = 30 / 2.0;
        txfSearchField.layer.masksToBounds = YES;
        txfSearchField.leftView = nil;//移除搜索按钮
    }
    return _searchBar;
}
-(void)setSearchBarBgColor:(UISearchBar *)mSearchBar{
    if ([[_searchBar subviews] count] > 0)
    {
        UIView * thesuperview = [[_searchBar subviews] objectAtIndex:0];
        NSArray *viewSubvies = [thesuperview subviews];
        for (UIView *_theview in viewSubvies)
        {
            if ([_theview isKindOfClass:[UITextField class]])
            {
            }
            if ([_theview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                [_theview removeFromSuperview];//去掉背景,否searchbar则会闪一下灰色背景
            }
        }
    }
}

你可能感兴趣的:(UISearchBar样式修改)