UISearchController更改“取消”按钮标题

时间:2015-02-21 06:16:44

标签: ios uitableview uisearchcontroller

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

enter image description here

11 个答案:

答案 0 :(得分:17)

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

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

答案 1 :(得分:8)

Swift相当于Burhanuddin Sunelwala的答案为我做了诀窍!

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

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

答案 2 :(得分:7)

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

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

答案 3 :(得分:3)

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

答案 4 :(得分:2)

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

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

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

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

答案 7 :(得分:1)

快速4和5

最适合我的简单方法是以下代码段:

class videoplayactivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video)

        val sanitizer = UrlQuerySanitizer(url)
        val value = sanitizer.getValue("parameter")

        val youtubefragment: YouTubePlayerSupportFragment = supportFragmentManager.findFragmentById(R.id.youtube_fragment)
        as YouTubePlayerSupportFragment
        youtubefragment.initialize("AIzaSyBs31eH16yzthuvHa0ItTdR_9pF6QFb3-U",object:YouTubePlayer.OnInitializedListener{
            override fun onInitializationSuccess(provider: YouTubePlayer.Provider?, player: YouTubePlayer?, wasRestored: Boolean){
                if(player == null) return
                if (wasRestored)
                    player.play()
                else{
                    player.cueVideo(value)
                    player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT)
                }
            }

            override fun onInitializationFailure(p0: YouTubePlayer.Provider?, p1: YouTubeInitializationResult?) {

            }
        })

    }


}

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

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

我希望这会有所帮助:)

答案 8 :(得分: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问题!

答案 9 :(得分:0)

快速 4.2、4.0 +

在答案here中添加了一个自定义class,该自定义-支持许多自定义,其结果如下。

enter image description here

答案 10 :(得分:0)

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