QQ里的几种searchBar

UISearchBar自定义

方式一.

QQ里的几种searchBar_第1张图片
屏幕快照 2016-09-23 15.01.18.png

//去掉阴影框线
self.searchBar.backgroundImage = [UIImage new];

//文字和搜索图标 在左边
- (void)setHasCentredPlaceholder:(UISearchBar *)searchBar
{
SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);
if ([self.searchBar respondsToSelector:centerSelector])
{
NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self.searchBar];
[invocation setSelector:centerSelector];
[invocation setArgument:&searchBar atIndex:2];
[invocation invoke];
}
}

xib:searcher的barTint和view的Tint的颜色设置white

方式二.

QQ里的几种searchBar_第2张图片
屏幕快照 2016-09-23 15.03.18.png

//修改背景

 //颜色——>Image
- (UIImage *)createImageWithColor: (UIColor *) color{    
     CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);                    
     UIGraphicsBeginImageContext(rect.size);    
     CGContextRef context = UIGraphicsGetCurrentContext();      
     CGContextSetFillColorWithColor(context, [color CGColor]);                                  
     CGContextFillRect(context, rect);
     UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();    
     UIGraphicsEndImageContext();  
     return theImage;
}

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
//修改搜索框的颜色
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.backgroundColor= [UIImage createImageWithColor:[UIColorclearColor]];;

NSArray *arr = searchBar.subviews;
for (UIView *subView in arr)
{
NSArray *arr2 = subView.subviews;
for (UIView *subView2 in arr2)
{
        if ([subView2 isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [subView2 removeFromSuperview];
            break;
        }
    }
}
UIView *view = [[UIView alloc] initWithFrame:searchBar.frame];
view.backgroundColor = [UIColor clearColor];
[searchBar insertSubview:view atIndex:0];

方式三.

屏幕快照 2016-09-23 15.05.23.png

xib:searchStyle为minimal

你可能感兴趣的:(QQ里的几种searchBar)