懶得自定義view 就用textfield直接寫了個搜索框,有個bug 就是在點(diǎn)擊右側(cè)搜索按鈕的時候或者點(diǎn)擊鍵盤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;
}
最關(guān)鍵的在于重寫下面的方法 ,編輯區(qū)域 ,leftview 和rightview 的區(qū)域
-(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;
}