借助textfield完成搜索

懒得自定义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;

}

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

推荐阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,144评论 1 6
  • 转自:http://www.code4app.com/blog-866962-1317.html1、设置UILab...
    MMOTE阅读 1,671评论 1 1
  • 在平时的沟通过程中,我们都希望达到一个境界:想清楚,说明白,知道怎么说,说什么。在评价一个人时,有一个标准是逻辑思...
    sophies阅读 888评论 4 4
  • 做好自己,永远不要活在别人的目光里。 2017年7月22日 星期六 晴 天生遗传父亲不爱说话的...
    宋使玉然阅读 237评论 6 2
  • 我已经很久很久没有一个人可托付。 曾吟过的那些诗句,亲自走过的路,哪怕是恰到好处拂在面上的一阵微风——都因为他赋予...
    萌萌哒阿吹阅读 355评论 0 0