//修改右侧返回按钮的语言
-(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遍历数组
/*
[[searchBar.subviews[0] subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isKindOfClass:[UIButton class]])
{
[obj setTitle:cancleTitle forState:UIControlStateNormal];
}
}];
*/
}
UISearchBar属性相关
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化,不解释
[self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符
[self.searchBar setPrompt:@"Prompt"];// 顶部提示文本,相当于控件的Title
[self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框样式
[self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的颜色,当设置此属性时,barStyle将失效
[self.searchBar setTranslucent:YES];// 设置是否透明
[self.searchBar setBackgroundImage:[UIImageimageNamed:@"image0"]];// 设置背景图片
[self.searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"image3"] forState:UIControlStateNormal];// 设置搜索框中文本框的背景
[self.searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"image0"] forState:UIControlStateHighlighted];
[self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30, 30)];// 设置搜索框中文本框的背景的偏移量
[self.searchBar setSearchResultsButtonSelected:NO];// 设置搜索结果按钮是否选中
[self.searchBar setShowsSearchResultsButton:YES];// 是否显示搜索结果按钮
[self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30,0)];// 设置搜索框中文本框的文本偏移量
[self.searchBar setInputAccessoryView:_btnHide];// 提供一个遮盖视图
[self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 设置键盘样式
// 设置搜索框下边的分栏条
[self.searchBar setShowsScopeBar:YES];// 是否显示分栏条
[self.searchBar setScopeButtonTitles:[NSArrayarrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分栏条,栏目
[self.searchBar setScopeBarBackgroundImage:[UIImageimageNamed:@"image3"]];// 分栏条的背景颜色
[self.searchBar setSelectedScopeButtonIndex:1];// 分栏条默认选中的按钮的下标
[self.searchBar setShowsBookmarkButton:YES];// 是否显示右侧的“书图标”
[self.searchBar setShowsCancelButton:YES];// 是否显示取消按钮
[self.searchBar setShowsCancelButton:YES animated:YES];
// 是否提供自动修正功能(这个方法一般都不用的)
[self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 设置自动检查的类型
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自动修正功能,一般设置为UITextAutocorrectionTypeDefault
self.searchBar.delegate = self;// 设置代理
[self.searchBar sizeToFit];
myTableView.contentInset =UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);
[self.view addSubview:myTableView];
[myTableView addSubview:self.searchBar];