项目中几乎肯定是要用搜索这项功能的,但是关于搜索框的圆角问题一直在困扰我!所以好好找了一下解决办法,上干货!
1.设置UISearchBar圆角
首先写一个UIImage的分类
#import <UIKit/UIKit.h>
@interface UIImage (XYColor)
/** 返回一个传入颜色值的iamge对象 */
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
@end
#import "UIImage+XYColor.h"
@implementation UIImage (XYColor)
/** 返回一个传入颜色值的iamge对象 */
+ (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;
}
@end
用的时候如下
searchBar.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(SCREEN_WIDTH - 80*WidthRatio, 30*WidthRatio)];
searchBar.layer.masksToBounds = YES;
searchBar.layer.cornerRadius = 15*WidthRatio;
这个时候你就会发现你的SearchBar完美设置了圆角
2.设置UISearchBar的placeholder的字体大小和颜色
UITextField *textfield = [searchBar valueForKey:@"_searchField"];
[textfield setValue:FontThin(14.0f*WidthRatio) forKeyPath:@"_placeholderLabel.font"];
[textfield setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
好了干货完毕,继续搬砖去了,喜欢的小伙伴可以点个赞在走呀~