7

我们如何更改搜索控制器中取消按钮的标题?

在此处输入图像描述

4

11 回答 11

18

上面提供的解决方案可能会随着新的 iOS 版本而改变。这是一个更好的方法:

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
于 2015-03-25T10:53:02.047 回答
11

相当于 Burhanuddin Sunelwala 的 Swift 答案对我有用!

self.searchBar.setValue("custom string", forKey: "cancelButtonText")

感谢 Burhanuddin Sunelwala 让我朝着正确的方向前进!

于 2016-08-02T13:44:58.367 回答
8

您可以使用此更改搜索栏中的“取消”按钮 -

for (UIView *view in searchBar.subviews)
{
    for (id subview in view.subviews)
    {
        if ( [subview isKindOfClass:[UIButton class]] )
        {
            [subview setEnabled:YES];
            UIButton *cancelButton = (UIButton*)subview;
            [cancelButton setTitle:@"hi" forState:UIControlStateNormal];


            NSLog(@"enableCancelButton");
            return;
        }
    }
}
于 2015-02-21T07:49:12.417 回答
4

值得注意的是,现在更改“取消”按钮标题的首选方法是通过外观(从另一个问题中得到这个想法:https ://stackoverflow.com/a/34522163/511878 ):

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];
于 2016-10-28T12:42:16.983 回答
3

您应该使用 UISearchBar 的外观代理。您可以在此处找到示例 -如何更改出现在 UISearchBar +iPhone 中的取消按钮的默认文本

于 2015-02-24T22:48:19.407 回答
2

快速版本:

for view:UIView in (searchView?.subviews)!
{
        for subView:UIView in (view.subviews)
        {
            if ( subView is UIButton )
            {
                let cancelBut = subView as! UIButton

                //do stuff with cancelButton here
            }
        }
    }
于 2016-02-22T11:15:46.657 回答
1
for (UIView *subView in SearchBar.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *cancelButton = (UIButton*)subView;

        [cancelButton setTitle:@"TitleString" forState:UIControlStateNormal];
}
于 2016-02-12T07:48:30.000 回答
1
  class ViewController: UIViewController,UISearchResultsUpdating {
        func updateSearchResults(for searchController: UISearchController) {
            //write your code here
        }
        @IBOutlet weak var navItem: UINavigationItem!
        let searchController = UISearchController(searchResultsController: nil)
        override func viewDidLoad() {
            super.viewDidLoad()
            searchController.obscuresBackgroundDuringPresentation = false
            searchController.definesPresentationContext = true
            searchController.searchResultsUpdater = self
            if #available(iOS 11.0, *){
                navigationItem.searchController = searchController
                UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "new title"
            }
            // Do any additional setup after loading the view.
        }
    }
于 2019-07-30T13:15:03.540 回答
1

快速 4 & 5

对我有用的最好和最简单的方法是这段代码:

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

您可以像这样修改更多属性:

if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton {
    cancelButton.setTitle(<your_string>, for: <UIControlState>)
    cancelButton.setTitleColor(<your_uicolor>, for: <UIControlState>)
    cancelButton.setAttributedTitle(<your_nsattributedstring>, for: <UIControlState>)
}

我希望这有帮助 :)

于 2020-05-22T02:33:44.297 回答
0

您还需要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 来避免 Apple 的问题!

于 2015-02-21T07:13:19.773 回答
0

斯威夫特4.2、4.0+

在此处的答案中添加了一个class支持许多自定义并具有以下结果的自定义。

在此处输入图像描述

于 2018-07-16T07:24:00.293 回答