简体   繁体   English

UISearchBar 问题中的取消按钮

[英]Cancel Button in UISearchBar issue

I have implemented search bar in my app with custom(localized) title.我在我的应用程序中实现了带有自定义(本地化)标题的搜索栏。

for the first time when I tap on search it shows cancel button as follow:当我第一次点击搜索时,它会显示如下取消按钮:

First Time第一次

在此处输入图片说明

Second Time When I press cancel button & again tap on Search Bar, it looks fine.第二次当我按下取消按钮并再次点击搜索栏时,它看起来不错。

在此处输入图片说明

My code我的代码

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

}

It's working for me:它对我有用:

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

Try setting your NSLocalizedString with UIAppearance :请尝试设置NSLocalizedStringUIAppearance

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

swift迅速

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

(which no longer works in modern ios) this one Change UISearchBar cancel button text in iOS 8 is up to date (在现代 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