8

我希望更改 iOS 中取消按钮的标题。我以前一直在使用这个:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
  self.searchDisplayController.searchBar.showsCancelButton = YES;
  UIButton *cancelButton = nil;
  for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
     if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
         cancelButton = (UIButton*)subView;
     }
  }
  [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
}

但它似乎不适用于iOS7。有什么建议么?

4

13 回答 13

12

您需要递归搜索按钮。这应该是一种故障安全的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self convertButtonTitle:@"Cancel" toTitle:@"Annuller" inView:self.searchBar];
}

- (void)convertButtonTitle:(NSString *)from toTitle:(NSString *)to inView:(UIView *)view
{
    if ([view isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton *)view;
        if ([[button titleForState:UIControlStateNormal] isEqualToString:from])
        {
            [button setTitle:to forState:UIControlStateNormal];
        }
    }

    for (UIView *subview in view.subviews)
    {
        [self convertButtonTitle:from toTitle:to inView:subview];
    }
}

我仅在 iOS 7 上对此进行了测试,但它也可以在 iOS 6 上运行并且应该这样做。

于 2013-09-23T09:58:35.380 回答
10

只需为它执行此代码:-

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    /* when user start editing in serchbar this method will display cancel button and disable the autocorrection functionality */

    srcbar.showsCancelButton = YES;

    for (UIView *subView in searchBar.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
           UIButton *cancelButton = (UIButton*)subView;

            [cancelButton setTitle:@"hi" forState:UIControlStateNormal];
        }
    }
    srcbar.autocorrectionType = UITextAutocorrectionTypeNo;

}

不在 iOS7 中测试,但在 iOS6 中工作正常希望这对你有用。

输出是:-

在此处输入图像描述

于 2013-09-23T10:06:21.743 回答
10

这是我对 ios6 和 ios7 的解决方案

#define IS_IOS7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    self.searchDisplayController.searchBar.showsCancelButton = YES;

    UISearchBar *searchBar = self.searchDisplayController.searchBar;
    UIView *viewTop = IS_IOS7 ? searchBar.subviews[0] : searchBar;
    NSString *classString = IS_IOS7 ? @"UINavigationButton" : @"UIButton";

    for (UIView *subView in viewTop.subviews) {
        if ([subView isKindOfClass:NSClassFromString(classString)]) {
            UIButton *cancelButton = (UIButton*)subView;
            [cancelButton setTitle:@"your title" forState:UIControlStateNormal];
        }
    }
}
于 2013-12-25T02:49:02.693 回答
8

为了让它在 iOS7 上运行,你必须在搜索栏的子视图中搜索:

//iOS 7 hack
searchBar.showsCancelButton = YES;
UIView* view=searchBar.subviews[0];
for (UIView *subView in view.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *cancelButton = (UIButton*)subView;

        [cancelButton setTitle:@"Anuluj" forState:UIControlStateNormal];
    }
}
于 2013-12-05T21:48:02.960 回答
4

将此行放入 appDelegate

[[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Your Title" forState:UIControlStateNormal];

于 2015-07-07T10:26:38.707 回答
4

对于 Swift 3.0

这工作正常。

func setSearchButtonText(text:String,searchBar:UISearchBar) {

    for subview in searchBar.subviews {
        for innerSubViews in subview.subviews {
            if let cancelButton = innerSubViews as? UIButton {
                cancelButton.setTitleColor(UIColor.white, for: .normal)
                cancelButton.setTitle(text, for: .normal)
            }
        }
    }

}

并调用方法

setSearchButtonText(text: "Done", searchBar: yourSearchBar)

这是输出

在此处输入图像描述

于 2016-10-26T10:58:27.597 回答
1

如果您只想在您的语言环境中将“取消”文本更改为相同,我认为正确的方法是使用本地化。方法如下:

  1. 在Finder中打开你的项目,找到en.lproj目录
  2. 将其重命名为您的语言环境,或者如果您必须支持多种语言则复制它
  3. 删除它可能包含在 Xcode 中的任何文件引用(它们应该看起来是红色的)
  4. 将 Finder 中的相同文件从新目录拖到 Xcode
  5. (不确定这是必要的)编辑您Info.plist并添加以下行:

    • Localization native development region:您所在的地区(例如en:)
    • Localizations: 例如English

注意:它在模拟器中不起作用,在设备上测试它!

(来源:http ://www.ibabbleon.com/iphone_app_localization.html )

于 2013-11-24T22:05:30.963 回答
0

在搜索开始之前,在 UISearchBarDelagate 或 UISearchDisplayControllerDelegate 中执行:

[self.searchDisplayController.searchBar.subviews enumerateObjectsUsingBlock:^(UIButton *subview, NSUInteger idx, BOOL *stop) {
            if ([subview isKindOfClass:[UIButton class]]) {
                [subview setTitle:@"OK" forState:UIControlStateNormal];
                *stop = YES;
            }
        }];
于 2013-10-24T12:01:18.153 回答
0

对于 iOS7,这是可行的。

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
    self.searchDisplayController.searchBar.showsCancelButton = YES;
    UIButton *cancelButton;
    UIView *topView = self.searchDisplayController.searchBar.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
        }
    }
    if (cancelButton) {
      //Set the new title of the cancel button
        [cancelButton setTitle:@"YourTitle" forState:UIControlStateNormal];
    }
}
于 2014-07-10T00:43:54.613 回答
0

@Nitin Gohel 喜欢吗?

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
  self.searchDisplayController.searchBar.showsCancelButton = YES;
  UIButton *cancelButton = nil;
  for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
     if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
         cancelButton = (UIButton*)subView;

         [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
     }
 }

}
于 2013-09-23T09:56:45.417 回答
0

似乎“self.searchDisplayController.searchBar.subviews”只是返回“searchBar”本身。我尝试了以下方法,它有效!

for (UIView *searBarView in [self.searchDisplayController.searchBar subviews])
{
    for (UIView *subView in [searBarView subviews]) 
    {
        if ([subView isKindOfClass:[UIButton class]])
        {
            UIButton *cancleButton = (UIButton *)subView;
            [cancleButton setTitle:@"hi" forState:UIControlStateNormal];
        }
    }

}
于 2013-09-23T10:39:57.887 回答
0

加入我在UIKitUISearchResultsUpdating协议方法中使用的代码updateSearchResults(for searchController: UISearchController)

if let cancelButton : UIButton = searchController.searchBar.value(forKey: "_cancelButton") as? UIButton {
    cancelButton.setTitle("Back", for: UIControlState.normal)
}

(iOS 10,斯威夫特 3.0)

于 2017-05-02T20:56:04.083 回答
0

参考上面@Salih的解决方案,我使用了这段代码并且工作完美!只需在应用启动时调用 AppDelegate.m 中的 setupAppearance 方法即可。

- (void)setupAppearance
{
   id appearance = nil;

   if (IOS9_OR_LATER) {
       appearance = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]];
   } else {
       appearance = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
   }

   [appearance setTitle:@"CANCEL"];
}
于 2016-04-22T02:33:42.713 回答