產(chǎn)品要求猖毫,如下圖台谍,在不觸發(fā)UISearchBar的情況下
1.默認(rèn)的提示文字居左
2.刪除按鈕默認(rèn)顯示,且樣式要替換
3.取消按鈕顯示
4.輸入光標(biāo)距最左邊指定間隔
5.隱藏放大鏡圖標(biāo)
- (void)addSearchBarView {
UIView *searchBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 75.f)];
searchBgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:searchBgView];
UISearchBar *searchBarView = [[UISearchBar alloc] initWithFrame:CGRectMake(8.f, 27.f,SCREEN_WIDTH-58.f, 44.f)];
//光標(biāo)顏色
searchBarView.tintColor = [UIColor grayColor];
[searchBarView setBackgroundImage:[UIImage imageNamed:@"clear_bg"]];
//背景圖片
[searchBarView setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchFiledBg"] forState:UIControlStateNormal];
//輸入光標(biāo)左邊距
[searchBarView setSearchTextPositionAdjustment: UIOffsetMake(10, 0)];
searchBarView.delegate = self;
self.searchBarView = searchBarView;
//隱藏放大鏡圖標(biāo)
UITextField *txfSearchField = [searchBarView valueForKey:@"_searchField"];
[txfSearchField setLeftViewMode:UITextFieldViewModeNever];
//自定義取消按鈕
UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-57.f, CGRectGetMinY(searchBarView.frame), 57.f, 44.f)];
[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
cancelBtn.titleLabel.font = [UIFont CFRegular:14.f];
[cancelBtn setTitleColor:RGBSame(28) forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(cancelSearch) forControlEvents:UIControlEventTouchUpInside];
//自定義placeholder text
UILabel *label = [UILabel new];
label.frame = CGRectMake(18, 8, 200, 28);
label.text = @"搜索Fashionista和文章";
label.font = [UIFont CFRegular:14];
label.textColor = HBGrayColor;
[searchBarView addSubview:label];
self.placeHolderLbl = label;
[searchBgView addSubview:searchBarView];
[searchBgView addSubview:cancelBtn];}
下一步就是顯示刪除按鈕了吁断,注意要在viewDidAppear方法里操作趁蕊,遍歷UISearchBar的子視圖找到searchField,自定義searchField.rightView胯府,然后searchField.rightViewMode = UITextFieldViewModeAlways;將默認(rèn)的清除按鈕狀態(tài)設(shè)為searchField.clearButtonMode = UITextFieldViewModeNever;代碼詳情如下:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
for(UIView *subView in self.searchBarView.subviews.firstObject.subviews) {
if([subView isKindOfClass: [UITextField class]]){
UITextField *searchField = (UITextField *)subView;
CGFloat myWidth = 26.0f;
CGFloat myHeight = 30.0f;
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myWidth, myHeight)];
[myButton setImage:[UIImage imageNamed:@"icon_delete_search_normal"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"icon_delete_search_normal"] forState:UIControlStateHighlighted];
[myButton addTarget:self action:@selector(clearsearchbar) forControlEvents:UIControlEventTouchUpInside];
searchField.rightView = myButton;
searchField.rightViewMode = UITextFieldViewModeAlways;
searchField.clearButtonMode = UITextFieldViewModeNever;
}
} }
當(dāng)然要記得在觸發(fā)UISearchBar時隱藏自定義的Placeholder介衔,并按照自己的需求去實現(xiàn)刪除和取消按鈕的觸發(fā)方法,下面是我的實現(xiàn)
- (void)clearsearchbar {
_searchBarView.text = nil;
self.placeHolderLbl.hidden = NO;
[_searchBarView resignFirstResponder];}
- (void)cancelSearch {
[_searchBarView resignFirstResponder];
[self.navigationController popViewControllerAnimated:YES];}