UISearchController 更改 'Cancel' 按钮标题

UISearchController change 'Cancel' button title

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

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

您可以使用此更改搜索栏中的 "Cancel" 按钮-

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;
        }
    }
}

您应该使用 UISearchBar 的外观代理。你可以在这里找到一个例子 - How to change the default text of Cancel Button which appears in the UISearchBar +iPhone

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

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
for (UIView *subView in SearchBar.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *cancelButton = (UIButton*)subView;

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

swift版本:

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
            }
        }
    }

Burhanuddin Sunelwala 的 Swift 答案对我有用!

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

感谢 Burhanuddin Sunelwala 为我指明了正确的方向!

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

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];

Swift 4.2, 4.0+

在答案 here 中添加了支持多种自定义的自定义 class,结果如下。

  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.
        }
    }

swift 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>)
}

希望对您有所帮助:)