searchBar的使用:

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,428评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,869评论 0 1
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,226评论 1 4
  • 我是勤劳的搬运工 我是勤劳的搬运工 我是勤劳的搬运工 我是勤劳的搬运工
    昵称很难注册阅读 184评论 0 0
  • 波波叔阅读 171评论 0 0