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 に答える
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];
        }
    }
}

また、Apple の問題を回避するために UIButton を使用することにも注意してください。

于 2015-02-21T07:13:19.773 に答える