懒得自定义view 就用textfield直接写了个搜索框,有个bug 就是在点击右侧搜索按钮的时候或者点击键盘return的时候push两个残影,(加在Navbar 上的 时候建议自定义Navbar);
#import@interface LBTextFild : UITextField
@property(nonatomic,strong)NSString *AttributeplaceHolder;
@property(nonatomic,strong)UIImage *leftImage;
@property(nonatomic,strong)UIImage *rightImage;
@end
#import "LBTextFild.h"
@interface LBTextFild()
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UIImageView *imageview;
@end
@implementation LBTextFild
-(instancetype)initWithFrame:(CGRect)frame
{
self =[super initWithFrame:frame];
if (self) {
self.layer.borderWidth = 1;
self.layer.cornerRadius = 15;
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor whiteColor].CGColor;
self.leftViewMode = UITextFieldViewModeAlways;
self.layer.borderColor = [UIColor whiteColor].CGColor;
self.imageview =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
self.imageview.image =[UIImage imageNamed:@"buy_type_search"];
self.leftView = self.imageview;
self.button =[UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(0, 0, 23, 23);
[self.button setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
self.rightView = self.button;
self.rightViewMode = UITextFieldViewModeAlways;
}
return self;
}
-(void)setLeftImage:(UIImage *)leftImage
{
self.imageview.image = leftImage;
self.leftView = self.imageview;
}
-(void)setRightImage:(UIImage *)rightImage
{
[self.button setImage:rightImage forState:UIControlStateNormal];
self.rightView = self.button;
}
-(void)setAttributeplaceHolder:(NSString *)AttributeplaceHolder
{
NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:15.0],NSFontAttributeName,
[UIColor whiteColor],NSForegroundColorAttributeName,
nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:AttributeplaceHolder attributes:attributeDict];
self.attributedPlaceholder = AttributedStr;
}
最关键的在于重写下面的方法 ,编辑区域 ,leftview 和rightview 的区域
-(CGRect)rightViewRectForBounds:(CGRect)bounds {
CGRect iconRect = [super rightViewRectForBounds:bounds];
iconRect.origin.x -= 10;// 右偏10
return iconRect;
}
-(CGRect)textRectForBounds:(CGRect)bounds
{
CGRect textRect =[super editingRectForBounds:bounds];
NSLog(@"%@",NSStringFromCGRect(bounds));
// editRect.origin.x+= 50;
textRect.origin.x += bounds.size.width/2 - 90;
textRect.size.width -= bounds.size.width/2 - 90 + self.rightView.frame.size.width;
return textRect;
}
-(CGRect)editingRectForBounds:(CGRect)bounds
{
CGRect editRect =[super editingRectForBounds:bounds];
// NSLog(@"%@",NSStringFromCGRect(bounds));
editRect.origin.x+= bounds.size.width/2 - 90;
editRect.size.width -= bounds.size.width/2 - 90 + self.rightView.frame.size.width;
return editRect;
}
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
CGRect placeholderRect =[super editingRectForBounds:bounds];
// NSLog(@"%@",NSStringFromCGRect(bounds));
placeholderRect.origin.x+= bounds.size.width/2 - 90;
return placeholderRect;
}