SearchBar、Tableview右边的英文A-Z背景色设置为任意颜色

来源:互联网 发布:demo软件 编辑:程序博客网 时间:2024/04/28 18:33

首先来一张效果图  如下




1、去掉UISearchBar的背景色代码如下

_searchbar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, navHeight, SCREEN_WIDTH, 40)];    _searchbar.backgroundColor = [RGB(244, 244, 244)colorWithAlphaComponent:.2f];    _searchbar.barStyle = UIBarStyleBlackTranslucent;    _searchbar.barTintColor = [RGB(244, 244, 244)colorWithAlphaComponent:.2f];    _searchbar.tintColor = [RGB(244, 244, 244)colorWithAlphaComponent:.2f];    _searchbar.autocorrectionType = UITextAutocorrectionTypeNo;    _searchbar.autocapitalizationType = UITextAutocapitalizationTypeNone;    _searchbar.placeholder = (@"Search");    _searchbar.delegate = self;    _searchbar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:_searchbar.bounds.size];    for (UIView *subview in _searchbar.subviews){                if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {            [subview removeFromSuperview];            break;        }    }    [self.view addSubview:_searchbar];

上面初始化代码可自行删减  设置背景色主要是imageWithColor方法

//取消searchbar背景色- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{    CGRect rect = CGRectMake(0, 0, size.width, size.height);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return image;}


2、去掉TableView右边英文字母的背景色(系统默认为白色)

_mainTBV = [[UITableView alloc]initWithFrame:CGRectMake(0, _searchbar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-_searchbar.bottom)];    _mainTBV.dataSource = self;    _mainTBV.delegate = self;    _mainTBV.backgroundColor = [UIColor clearColor];    _mainTBV.separatorStyle = UITableViewCellSeparatorStyleNone;    [_mainTBV setAllowsMultipleSelection:YES];    _mainTBV.bounces = NO;    _mainTBV.sectionIndexBackgroundColor = [UIColor clearColor];    [self.view addSubview:_mainTBV];

sectionIndexBackgroundColor 就是设置TableView的背景色  我这设置成无色


原创粉丝点击