//修改右側(cè)返回按鈕的語(yǔ)言
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_searchBar setShowsCancelButton:YES animated:YES];
[self chanageSearchBar:searchBar cancleTitle:@"取消"];
}
-(void)chanageSearchBar:(UISearchBar *)searchBar
cancleTitle:(NSString *)cancleTitle
{
for (UIView *view in [searchBar.subviews[0] subviews]) {
if([view isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)view;
[button setTitle:cancleTitle forState:UIControlStateNormal];
}
}
//用block遍歷數(shù)組
/*
[[searchBar.subviews[0] subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isKindOfClass:[UIButton class]])
{
[obj setTitle:cancleTitle forState:UIControlStateNormal];
}
}];
*/
}
UISearchBar屬性相關(guān)
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化,不解釋
[self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符
[self.searchBar setPrompt:@"Prompt"];// 頂部提示文本,相當(dāng)于控件的Title
[self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框樣式
[self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的顏色,當(dāng)設(shè)置此屬性時(shí),barStyle將失效
[self.searchBar setTranslucent:YES];// 設(shè)置是否透明
[self.searchBar setBackgroundImage:[UIImageimageNamed:@"image0"]];// 設(shè)置背景圖片
[self.searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"image3"] forState:UIControlStateNormal];// 設(shè)置搜索框中文本框的背景
[self.searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"image0"] forState:UIControlStateHighlighted];
[self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30, 30)];// 設(shè)置搜索框中文本框的背景的偏移量
[self.searchBar setSearchResultsButtonSelected:NO];// 設(shè)置搜索結(jié)果按鈕是否選中
[self.searchBar setShowsSearchResultsButton:YES];// 是否顯示搜索結(jié)果按鈕
[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30,0)];// 設(shè)置搜索框中文本框的文本偏移量
[self.searchBar setInputAccessoryView:_btnHide];// 提供一個(gè)遮蓋視圖
[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 設(shè)置鍵盤樣式
// 設(shè)置搜索框下邊的分欄條
[self.searchBar setShowsScopeBar:YES];// 是否顯示分欄條
[self.searchBar setScopeButtonTitles:[NSArrayarrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分欄條,欄目
[self.searchBar setScopeBarBackgroundImage:[UIImageimageNamed:@"image3"]];// 分欄條的背景顏色
[self.searchBar setSelectedScopeButtonIndex:1];// 分欄條默認(rèn)選中的按鈕的下標(biāo)
[self.searchBar setShowsBookmarkButton:YES];// 是否顯示右側(cè)的“書圖標(biāo)”
[self.searchBar setShowsCancelButton:YES];// 是否顯示取消按鈕
[self.searchBar setShowsCancelButton:YES animated:YES];
// 是否提供自動(dòng)修正功能(這個(gè)方法一般都不用的)
[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 設(shè)置自動(dòng)檢查的類型
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自動(dòng)修正功能,一般設(shè)置為UITextAutocorrectionTypeDefault
self.searchBar.delegate = self;// 設(shè)置代理
[self.searchBar sizeToFit];
myTableView.contentInset =UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);
[self.view addSubview:myTableView];
[myTableView addSubview:self.searchBar];