代码之家  ›  专栏  ›  技术社区  ›  Pradeep Reddy Kypa

如何更改UISearchBar+iPhone中出现的“取消”按钮的默认文本

  •  26
  • Pradeep Reddy Kypa  · 技术社区  · 16 年前

    我正在开发一个应用程序,我想改变搜索栏中搜索字符串的文本。我想改变文本取消按钮也出现在旁边的搜索栏。在搜索栏中输入任何字符串之前,我们会将搜索字符串作为默认字符串。我想更改字符串的文本,当我们点击搜索栏时,在搜索栏旁边有一个取消按钮,我想更改取消按钮的文本。

    14 回复  |  直到 7 年前
        1
  •  36
  •   Filip Radelic    13 年前

    在执行此过程之前,还需要使用“searchBar setShowsCancelButton”。

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        [theSearchBar setShowsCancelButton:YES animated:NO];
        for (UIView *subView in theSearchBar.subviews){
            if([subView isKindOfClass:[UIButton class]]){
                [(UIButton*)subView setTitle:@"Done" forState:UIControlStateNormal];
            }
        }
    }
    

    注意:使用UIButton避免苹果出现问题!

        2
  •  51
  •   Nikolay Shubenkov Grimxn    10 年前

    使用外观代理:

    id barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
    
    [barButtonAppearanceInSearchBar setBackgroundImage:grayBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [barButtonAppearanceInSearchBar setTitleTextAttributes:@{
                                          NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:20],
                                     NSForegroundColorAttributeName : [UIColor blackColor]
         } forState:UIControlStateNormal];
    [barButtonAppearanceInSearchBar setTitle:@"X"];
    
        3
  •  23
  •   kirans_6891 SGI    12 年前

    Mr. Jesper Nielsen -他写了密码。

    -(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
        UIButton *cancelButton;
        UIView *topView = theSearchBar.subviews[0];
        for (UIView *subView in topView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
                cancelButton = (UIButton*)subView;
            }
        }
        if (cancelButton) {
            [cancelButton setTitle:@"YourTitle" forState:UIControlStateNormal];
        }
    
    }
    
        4
  •  4
  •   glorifiedHacker    16 年前

    如果“Search String”是指占位符,那么应该这样做:

    [searchBar setPlaceholder:@"Whatever you want"];
    

    for(UIView *view in [searchBar subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            [(UIBarItem *)view setTitle:@"Whatever you want"];
        }
    }
    

    请注意,cancel按钮是延迟加载的,因此您必须在用户激活搜索栏时进行此修改。

        5
  •  4
  •   Mahesh Kumar    11 年前

    在ios7中,如果您使用UISearchBar,只需在searchBarTextDidBeginEditing:method中编写以下代码

    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:@"إلغاء" forState:UIControlStateNormal];
           }
    }
    
        6
  •  3
  •   Cœur Gustavo Armenta    12 年前

    我想修复UIAppearance技术,因为yar1vn代码不能与xcode5一起工作。

    首先,您需要了解cancel按钮是一个私有的UINavigationButton:UIButton。因此,它不是一个uibarbuttonite。经过一些检查后,UINavigationButton似乎会响应那些UIAppearance选择器:

    // inherited from UINavigationButton
    @selector(setTintColor:)
    @selector(setBackgroundImage:forState:style:barMetrics:)
    @selector(setBackgroundImage:forState:barMetrics:)
    @selector(setTitleTextAttributes:forState:)
    @selector(setBackgroundVerticalPositionAdjustment:forBarMetrics:)
    @selector(setTitlePositionAdjustment:forBarMetrics:)
    @selector(setBackButtonBackgroundImage:forState:barMetrics:)
    @selector(setBackButtonTitlePositionAdjustment:forBarMetrics:)
    @selector(setBackButtonBackgroundVerticalPositionAdjustment:forBarMetrics:)
    
    // inherited from UIButton
    @selector(setTitle:forState:)
    

    /* dual appearance technique by Cœur to customize a UINavigationButton */
    Class barClass = [UISearchBar self];
    
    UIBarButtonItem<UIAppearance> *barButtonItemAppearanceInBar = [UIBarButtonItem appearanceWhenContainedIn:barClass, nil];
    [barButtonItemAppearanceInBar setTintColor:...];
    [barButtonItemAppearanceInBar setBackgroundImage:... forState:... style:... barMetrics:...];
    [barButtonItemAppearanceInBar setBackgroundImage:... forState:... barMetrics:...];
    [barButtonItemAppearanceInBar setTitleTextAttributes:... forState:...];
    [barButtonItemAppearanceInBar setBackgroundVerticalPositionAdjustment:... forBarMetrics:...];
    [barButtonItemAppearanceInBar setTitlePositionAdjustment:... forBarMetrics:...];
    [barButtonItemAppearanceInBar setBackButtonBackgroundImage:... forState:... barMetrics:...];
    [barButtonItemAppearanceInBar setBackButtonTitlePositionAdjustment:... forBarMetrics:...];
    [barButtonItemAppearanceInBar setBackButtonBackgroundVerticalPositionAdjustment:... forBarMetrics:...];
    
    UIButton<UIAppearance> *buttonAppearanceInBar = [UIButton appearanceWhenContainedIn:barClass, nil];
    [buttonAppearanceInBar setTitle:... forState:...];
    

    [UINavigationBar self]

        7
  •  2
  •   Alexander Karpov    10 年前

    此解决方案适用于我-iOs7和iOs8:

    @interface ... : ...
    @property (strong, nonatomic) IBOutlet UISearchBar *search; 
    @end
    

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    
    NSArray *searchBarSubViews = [[self.search.subviews objectAtIndex:0] subviews];
    UIButton *cancelButton;
    for (UIView *subView in searchBarSubViews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
            break;
        }
    }
    if (cancelButton) {
    
        [cancelButton setTitle:@"New cancel" forState:UIControlStateNormal];
    
    }
     //insert this two lines below if you have a button appearance like this "Ne...cel" 
    
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar setShowsCancelButton:YES animated:YES]; 
    }
    
        8
  •  1
  •   followben    12 年前

    在iOS 7上,如果您设置了 displaysSearchBarInNavigationBar = YES UISearchDisplayController ,通过子视图递归或外观代理替换“取消”按钮标题 不起作用 .

    相反,在中使用自己的工具栏按钮 viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
        UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"A Custom Title", nil)
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(cancelButtonTapped:)];
    
        // NB: Order is important here.
        //     Only do this *after* setting displaysSearchBarInNavigationBar to YES
        //     as that's when UISearchDisplayController creates it's navigationItem
        self.searchDisplayController.navigationItem.rightBarButtonItem = barItem;
    }
    
        9
  •  1
  •   Pilgerstorfer Franz    10 年前

    杰里米·特里普在斯威夫特的工作代码

    func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
        self.searchDisplayController?.searchBar.showsCancelButton = true
        var cancelButton: UIButton
        var topView: UIView = self.searchDisplayController?.searchBar.subviews[0] as UIView
        for subView in topView.subviews {
            if subView.isKindOfClass(NSClassFromString("UINavigationButton")) {
            cancelButton = subView as UIButton
            cancelButton.setTitle("My Custom Title", forState: UIControlState.Normal)
            }
        }
    }
    
        10
  •  1
  •   Itachi    10 年前

    默认的“取消”标题为“取消”按钮,我更喜欢更改的值 循环流化床开发区 从en到项目中Info.plist文件中的本地化区域的键。

    这是找我的钱,

    <key>CFBundleDevelopmentRegion</key>
    <string>zh_CN</string>
    

    之后,默认的“取消”标题将显示为“中文”。此更改还将影响所有默认区域值,例如,UITextField/UITextView上粘贴板操作的操作标题将被本地化,“选择”->“,”粘贴“->”è"...

    顺便说一下,Info.plist文件可以完全本地化。

        11
  •  0
  •   CharlieMezak    13 年前

    我没有引用非公共的UINavigationButton类,而是执行了以下操作。我希望它能通过应用商店审查!

    for (id subview in searchBar.subviews) {
        if ([subview respondsToSelector:@selector(setTitle:)]) {
            [subview setTitle:@"Map"];
        }
    }
    
        12
  •  0
  •   jeremytripp    11 年前

    -(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:@"Hi" forState:UIControlStateNormal];
        }
    }
    
        13
  •  0
  •   Özgür    10 年前

    如果搜索栏在导航栏中,代码将与通常的答案不同;您需要搜索NavigationBar的子视图。

    -(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
    UINavigationBar * navigationBar =  self.navigationController.navigationBar;
    
    for (UIView *subView in navigationBar.subviews){
        if([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]){
            [(UIButton*)subView setTitle:@"İptal" forState:UIControlStateNormal];
        }
    }}
    

    在iOS7+中,如果你仍然不能设置标题,你应该学习视图调试-这就是我解决这个问题的方法。

    这篇简短的教程很好地概述了调试的关键观点:

    http://www.raywenderlich.com/98356/view-debugging-in-xcode-6

        14
  •  0
  •   sam k    6 年前
        if #available(iOS 13.0, *) {
            controller.searchBar.setValue("Done", forKey:"cancelButtonText")
        } else {
            controller.searchBar.setValue("Done", forKey:"_cancelButtonText")
        }
    

    ¤

    controller.searchBar.setValue("Done", forKey:"cancelButtonText") 适用于所有iOS版本

        15
  •  -1
  •   Andriy Zymenko    10 年前

    Swift 2.1中的工作短代码(已测试iOS7-9)

    @IBOutlet weak var searchBar: UISearchBar!
    func enableSearchBarCancelButton(enable: Bool, title: String? = nil) {
        searchBar?.showsCancelButton = enable
        if enable {
            if let _cancelButton = searchBar?.valueForKey("_cancelButton"),
                let cancelButton = _cancelButton as? UIButton {
                    cancelButton.enabled = enable //comment out if you want this button disabled when keyboard is not visible
                    if title != nil {
                        cancelButton.setTitle(title, forState: UIControlState.Normal)
                    }
            }
        }
    }