借助textfield完成搜索

懶得自定義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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、禁止手機(jī)睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa閱讀 1,212評論 1 6
  • 轉(zhuǎn)自:http://www.code4app.com/blog-866962-1317.html1、設(shè)置UILab...
    MMOTE閱讀 1,910評論 1 1
  • 在平時的溝通過程中,我們都希望達(dá)到一個境界:想清楚,說明白,知道怎么說,說什么。在評價一個人時,有一個標(biāo)準(zhǔn)是邏輯思...
    sophies閱讀 956評論 4 4
  • 做好自己,永遠(yuǎn)不要活在別人的目光里。 2017年7月22日 星期六 晴 天生遺傳父親不愛說話的...
    宋使玉然閱讀 262評論 6 2
  • 我已經(jīng)很久很久沒有一個人可托付。 曾吟過的那些詩句,親自走過的路,哪怕是恰到好處拂在面上的一陣微風(fēng)——都因?yàn)樗x予...
    萌萌噠阿吹閱讀 379評論 0 0

友情鏈接更多精彩內(nèi)容