簡體   English   中英

UISearchBar 問題中的取消按鈕

[英]Cancel Button in UISearchBar issue

我在我的應用程序中實現了帶有自定義(本地化)標題的搜索欄。

當我第一次點擊搜索時,它會顯示如下取消按鈕:

第一次

在此處輸入圖片說明

第二次當我按下取消按鈕並再次點擊搜索欄時,它看起來不錯。

在此處輸入圖片說明

我的代碼

   - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{

    [searchBar setShowsCancelButton:YES animated:YES];


    UIButton *cancelButton;
    UIView *topView = search.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
        }
    }
    if (cancelButton) {
        [cancelButton setTitle:Localized(@"Cancel") forState:UIControlStateNormal];
    }

}

它對我有用:

if (cancelButton) {
    [cancelButton setTitle:Localized(@"Cancel") forState:UIControlStateNormal];
    [search setNeedsLayout];
    [search layoutIfNeeded];
}

請嘗試設置NSLocalizedStringUIAppearance

[[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:Localized(@"Cancel") forState:UIControlStateNormal];

迅速

UIButton.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitle("v1_cancel", for: .normal)

(在現代 ios 中不再有效)這一項在 iOS 8 中更改 UISearchBar 取消按鈕文本是最新的

我在 swift 4 中翻譯了Jasper 的答案:

UIButton.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitle("Cancel", for: .normal)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM