美文网首页
UiSearchBar圆角设置

UiSearchBar圆角设置

作者: 半桶水技术 | 来源:发表于2017-03-10 17:39 被阅读0次

有时我们可能会遇到这样的需求:如图

搜索框两边是圆形的,当然这样的效果有很多方法来实现,但是个人感觉有点麻烦,今天我们用UISearchBar来实现上面的效果;具体实现思路是我们得到UISearchBar的子视图UITextField来设置其圆角,在系统的UISearchBar中UITextfield是这样的命名的searchField所以我们只要利用KVC得到就可以,具体实现代码如下:

[objc]view plaincopy

UITextField*searchField = [selfvalueForKey:@"searchField"];

if(searchField) {

[searchFieldsetBackgroundColor:[UIColorwhiteColor]];

searchField.layer.cornerRadius=14.0f;//设置圆角具体根据实际情况来设置

searchField.layer.borderColor= [UIColorlightGrayColor].CGColor;//边框的颜色

searchField.layer.borderWidth=1;//边框的宽

searchField.layer.masksToBounds=YES;

}

这样我们就可以给UISearchBar设置成圆角了,很简单吧 !

我们还可以给它加个背景图片来去掉系统的使看起开更美观,代码如下:

[objc]view plaincopy

UIImage* searchBarBg = [ToolsGetImageWithColor:[UtilsgetColor:@BACKGROUND]andHeight:40.0f];

UITextField*searchField = [selfvalueForKey:@"searchField"];

if(searchField) {

[searchFieldsetBackgroundColor:[UIColorwhiteColor]];

searchField.layer.cornerRadius=14.0f;

searchField.layer.borderColor= [UIColorlightGrayColor].CGColor;

searchField.layer.borderWidth=1;

searchField.layer.masksToBounds=YES;

}

[selfsetBackgroundImage:searchBarBg];

self.searchBarStyle= UISearchBarStyleProminent;

至于这个方法

[Tools GetImageWithColor:[Utils getColor:@BACKGROUND] andHeight:40.0f];是另外写出来的工具代码如下:

[objc]view plaincopy

+ (UIImage*)GetImageWithColor:(UIColor*)colorandHeight:(CGFloat)height

{

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

UIGraphicsBeginImageContext(r.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [colorCGColor]);

CGContextFillRect(context, r);

UIImage*img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnimg;

}

只要传进需要的颜色和高度就可自动生成UIImage;至于

UITextField *searchField = [self valueForKey:@"searchField"];怎么知道是searchField 大家可根据runtime得到答案;技术有限就写到这吧,希望大家多多指教!也可关注iOS半桶水技术公众号;实时收到我这个菜鸟写的文章

相关文章

  • UiSearchBar圆角设置

    有时我们可能会遇到这样的需求:如图 搜索框两边是圆形的,当然这样的效果有很多方法来实现,但是个人感觉有点麻烦,今天...

  • iOS UISearchBar 设置圆角以及设置placehol

    项目中几乎肯定是要用搜索这项功能的,但是关于搜索框的圆角问题一直在困扰我!所以好好找了一下解决办法,上干货! 1....

  • Image

    直接圆角图片 设置圆角图片度数 设置圆角图片带灰色圆角边框 设置圆角图片带灰色圆角边框带阴影

  • UISearchBar常见问题

    UISearchBar 1.UISearchBar右边设置语音搜索按钮 2.UISearchBar取消按钮,自定义文字

  • 改 UISearchBar 圆角的小技巧

    有些需求非要把 UISearchBar 默认的圆角矩形的圆角改大,顶端改成圆形的。虽然系统没有提供这个 API,不...

  • UISearchbar切圆角

    先看效果 ios13之后多了个新的属性:searchTextField 所以iOS13之后的可以直接拿 代码如下 ...

  • iOS 设置UI控件圆角

    一: 设置UIView上方圆角或者下方圆角 //设置UITableView section 圆角 设置好以后就这样了

  • iOS Masonry布局(四) - 视图设置圆角

    视图设置任意圆角 Masonry布局视图设置圆角 若使用Masonry布局的视图设置后发现,设置的圆角不起作用。这...

  • iOS Masonry - 视图设置圆角

    视图设置任意圆角 Masonry布局视图设置圆角 若使用Masonry布局的视图设置后发现,设置的圆角不起作用。这...

  • 设置UISearchBar

    更改searchBar的背景颜色及搜索框的颜色 - (UISearchBar *)searchBar{ if (!...

网友评论

      本文标题:UiSearchBar圆角设置

      本文链接:https://www.haomeiwen.com/subject/kzgxgttx.html