您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 程式設計技術

城市選擇之自訂搜索框

編輯:iOS 程式設計技術

新專案要做城市選擇,效果圖出來後發現用系統的搜索框達不到效果圖的樣子:設定 searchBarStyle 屬性時,UISearchBarStyleDefault 下設定 barTintColor 後邊框會有黑線;設定 UISearchBarStyleMinimal 時雖無黑線,但單個文本框的顏色又不符合要求。於是就自己封裝了一個。先看下效果圖。

上面兩個效果圖的區別在於:下面那張圖多了個城市和圖片,所以就把它們封裝到一個類中。本質上只是 TextField 的 LeftView 變化。還有個問題是城市名長短不一,設定城市時要進行重繪,所以我在 set 城市名時呼叫重繪方法。具體程式碼如下:

// // SearchTextField.h // LocationDemo // // Created by City--Online on 15/11/30. // Copyright © 2015年 City--Online. All rights reserved. // #import <UIKit/UIKit.h> typedef void(^LeftBtnClickBlock)(); @interface SearchTextField : UITextField @property (nonatomic,strong) NSString *leftTitle; @property (nonatomic,copy) LeftBtnClickBlock btnClickBlock; -(instancetype)initNoLeftTitleWithFrame:(CGRect)frame; @end
// // SearchTextField.m // LocationDemo // // Created by City--Online on 15/11/30. // Copyright © 2015年 City--Online. All rights reserved. // #import "SearchTextField.h" #import "Global.h" @interface SearchTextField () @property (nonatomic,strong) UIButton *leftBtn; @property (nonatomic,strong) UIImageView *leftImgView; @property (nonatomic,strong) UIImageView *searchImgView; @property (nonatomic,assign) BOOL isNoLeftTitle; @end @implementation SearchTextField -(instancetype)initNoLeftTitleWithFrame:(CGRect)frame { _isNoLeftTitle=YES; return [self initWithFrame:frame]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.layer.cornerRadius=5; self.layer.borderWidth=0.5; self.layer.borderColor=XQBColorLineLight.CGColor; self.leftViewMode=UITextFieldViewModeAlways; self.clearButtonMode=UITextFieldViewModeWhileEditing; self.backgroundColor=[UIColor whiteColor]; self.leftView=[[UIView alloc]init]; _leftBtn =[UIButton buttonWithType:UIButtonTypeSystem]; [_leftBtn addTarget:self action:@selector(btnClickHandle:) forControlEvents:UIControlEventTouchUpInside]; [_leftBtn setTitleColor:XQBColorTextMostLight forState:UIControlStateNormal]; [_leftBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; _leftImgView=[[UIImageView alloc]init]; _leftImgView.image=[UIImage imageNamed:@"login_location_down.png"]; [_leftBtn addSubview:_leftImgView]; [self.leftView addSubview:_leftBtn]; _searchImgView=[[UIImageView alloc]init]; _searchImgView.image=[UIImage imageNamed:@"login_location_search.png"]; [self.leftView addSubview:_searchImgView]; } return self; } -(void)layoutSubviews { [super layoutSubviews]; if (_isNoLeftTitle) { self.leftView.frame=CGRectMake(0, 0, 35, self.frame.size.height); _leftBtn.frame=CGRectMake(0, 0, 0, 0); _leftImgView.frame=CGRectMake(0, 0, 0, 0); _searchImgView.frame=CGRectMake(10, (self.frame.size.height-15)/2, 15, 15); } else{ NSDictionary *attributes=@{NSFontAttributeName: XQBFontMiddle}; CGRect titleFrame=[_leftTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; self.leftView.frame=CGRectMake(0, 0, 62+titleFrame.size.width, self.frame.size.height); _leftBtn.frame=CGRectMake(10, 0, titleFrame.size.width+22, self.frame.size.height); [_leftBtn setTitle:_leftTitle forState:UIControlStateNormal]; _leftImgView.frame=CGRectMake(titleFrame.size.width+10, (self.frame.size.height-5)/2, 10, 5); _searchImgView.frame=CGRectMake(titleFrame.size.width+37, (self.frame.size.height-15)/2, 15, 15); } } -(void)btnClickHandle:(id)sender { _btnClickBlock(); } -(void)setLeftTitle:(NSString *)leftTitle { _leftTitle=leftTitle; _isNoLeftTitle=NO; [self setNeedsLayout]; } @end
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved