简体   繁体   English

UISearchController 更改“取消”按钮标题

[英]UISearchController change 'Cancel' button title

How we can change the title of cancel button in search controller?我们如何更改搜索控制器中取消按钮的标题?

在此处输入图片说明

The solution provided above could change with new iOS releases.上面提供的解决方案可能会随着新的 iOS 版本而改变。 Here is a better approach:这是一个更好的方法:

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];

The Swift equivalent of Burhanuddin Sunelwala answer did the trick for me! Swift 相当于 Burhanuddin Sunelwala 的回答对我有用!

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

Thanks to Burhanuddin Sunelwala for putting me in the right direction!感谢 Burhanuddin Sunelwala 让我朝着正确的方向前进!

You can change the "Cancel" Button in search bar using this-您可以使用此更改搜索栏中的“取消”按钮 -

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

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

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

You should use the appearance proxy of the UISearchBar.您应该使用 UISearchBar 的外观代理。 You can find an example here - How to change the default text of Cancel Button which appears in the UISearchBar +iPhone你可以在这里找到一个例子 - 如何更改出现在 UISearchBar +iPhone 中的取消按钮的默认文本

swift version:快速版本:

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
            }
        }
    }
for (UIView *subView in SearchBar.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *cancelButton = (UIButton*)subView;

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

swift 4 & 5快速 4 & 5

The best and simple way that worked for me is this snippet of code:对我有用的最好和最简单的方法是这段代码:

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

You can modify more properties like this:您可以像这样修改更多属性:

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

I hope this helps :)我希望这有帮助 :)

You also need to have the searchBar setShowsCancelButton before the procedure.您还需要在程序之前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];
        }
    }
}

Note also use UIButton to avoid problems with Apple!注意还要使用 UIButton 来避免 Apple 的问题!

Swift 4.2, 4.0+斯威夫特4.2、4.0+

A custom class that supports many customizations with the below results is added in the answer here .此处的答案中添加了一个自定义classclass支持具有以下结果的许多自定义项。

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM