我无法更改搜索栏取消按钮的文本。
请参阅我的代码:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_srBar setShowsCancelButton:YES animated:YES];
UIButton *cancelButton = nil;
for (UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
cancelButton = (UIButton*)subView;
}
}
[cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
}
此外,我已经尝试了其他选项
iOS Change the title of cancel button in UISearchBar iOS - Customizing Cancel button of UISearchBar
在此示例中,我的代码不会调用方法
How to change the default text of Cancel Button which appears in the UISearchBar +iPhone
我的风景
#import <UIKit/UIKit.h>
@interface favorito : UIViewController<UISearchControllerDelegate, UISearchDisplayDelegate, UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
@end
答案 0 :(得分:1)
id barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
[barButtonAppearanceInSearchBar setBackgroundImage:grayBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[barButtonAppearanceInSearchBar setTitleTextAttributes:@{
UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:20],
UITextAttributeTextColor : [UIColor blackColor]
} forState:UIControlStateNormal];
[barButtonAppearanceInSearchBar setTitle:@"X"];