searchBar的使用:

2017-01-04  本文已影响10人  violafa

1.设置系统默认的背景框,圆角等属性:推荐网址1:http://www.cnblogs.com/-ios/p/5948643.html

推荐网址2:http://www.jianshu.com/p/57ba04b7e389

以下是个人实现原理:

UISearchBar *sbSearchBar =[[UISearchBar alloc]init];

/**设置背景色*/

UIImage* searchBarBg =[self GetImageWithColor:[UIColor clearColor]andHeight:32.0f];

//设置背景图片

[sbSearchBar setBackgroundImage:searchBarBg];

//设置背景色

[sbSearchBar setBackgroundColor:[UIColor clearColor]];

//更改search圆角

UITextField *searchField =[sbSearchBar valueForKey:@"searchField"];

if(searchField){

[searchField setBackgroundColor:[UIColor whiteColor]];

searchField.layer.cornerRadius = 2.0f;

searchField.layer.borderColor =[UIColor sam_colorWithHex:@"d8d8d8"].CGColor;

searchField.layer.borderWidth = 1;

searchField.layer.masksToBounds = YES;

}

sbSearchBar.placeholder = @"请输入关键字";

[self.view addSubview:sbSearchBar];

[sbSearchBar mas_makeConstraints:^(MASConstraintMaker *make){

make.left.mas_equalTo(scanBtn.mas_right);

make.top.bottom.right.mas_equalTo(scanOrSearchV);

}];

-(UIImage*)GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height

{

CGRect r= CGRectMake(0.0f,0.0f,1.0f,height);

UIGraphicsBeginImageContext(r.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context,[color CGColor]);

CGContextFillRect(context,r);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;

}

上一篇下一篇

猜你喜欢

热点阅读