告诉我如何更改searchBar中“取消”按钮上的文字?
答案 0 :(得分:2)
你可以在UISearchBar的这个委托方法中做到这一点
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
[theSearchBar setShowsCancelButton:YES animated:NO];
for (UIView *subView in theSearchBar.subviews){
if([subView isKindOfClass:[UIButton class]]){
[(UIButton*)subViewsetTitle:@"Button Title"forState:UIControlStateNormal];
}
}
}
<强> 更新 强>
经过很长一段路搜索我在swift工作的唯一途径之后是设置自定义UIBarButtonItem,但您需要在导航上显示搜索栏
在ViewDidLoad()
中 self.searchDisplayController?.displaysSearchBarInNavigationBar = true
并在Delegate Method
中func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
var barButton = UIBarButtonItem(title: "Button Title", style: UIBarButtonItemStyle.Done, target: self, action: "here")
self.searchDisplayController?.navigationItem.rightBarButtonItem = barButton
}
我希望能与你合作
«另一个更新
正如您在评论中所说,您将需要本地化您的应用程序,在您的情况下,您只需要本地化storyBoard
首先导航到您的项目设置然后信息,在本地化下单击+按钮并添加您自己的语言然后仅检查StoryBoard
现在您已对应用进行了本地化,但在删除应用并再次安装之前可能看不到更改,或者如果将设备语言设置为英语,则需要编写2行代码来更改语言这里手动是
var str:NSString = "ar" // ar stands for arabic you put here you own language small character
var myArray:NSArray = [str]
NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: "AppleLanguages")
,您的按钮将如下所示
如果您想了解有关本地化的更多信息,请参阅此Internationalization Tutorial for iOS [2014 Edition]
如果你还需要帮助,直到我:)
答案 1 :(得分:0)
有两种解决方案。
[self.searchController.searchBar setValue:@&#34;취소&#34; forKey:@&#34; _cancelButtonText&#34;];
此解决方案效果很好,但您必须知道_cancelButtonText属性不是公共属性。(您无法在文档页面中找到此属性。) 并且不确定此解决方案是否可以通过苹果审核流程。所以,请使用解决方案2.
(假设searchBar在tableView headerView中)
- (void)willPresentSearchController:(UISearchController *)searchController{
// You have to set YES showsCancelButton.
// If not, you can not change your button title when this method called
// first time.
self.searchController.searchBar.showsCancelButton = YES;
UIView* view=self.searchController.searchBar.subviews[0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
// solution 1
UIButton *cancelButton = (UIButton*)subView;
[cancelButton setTitle:NSLocalizedString(@"취소", nil) forState:UIControlStateNormal];
}
}
}
此外,您可以通过Xcode中的视图调试工具获取有关searchBar的信息。在willPresentSearchController方法中设置breakPoint。 祝你好运。
答案 2 :(得分:0)
实际上,取消按钮不是搜索栏中的顶视图。你应该递归地搜索它。
-(UIButton*)findButtonInView:(UIView*)v{
for (UIView *subView in v.subviews){
if([subView isKindOfClass:[UIButton class]]){
return (UIButton*)subView;
}else if([subView isKindOfClass:[UIView class]]){
UIButton* btn = [self findButtonInView:subView];
if(btn){
return btn;
}
}
}
return nil;
}
//...
UIButton* searchButton = [self findButtonInView:self.searchBar];