我希望在iOS中更改取消按钮的标题。我之前一直在使用它:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIButton *cancelButton = nil;
for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
cancelButton = (UIButton*)subView;
}
}
[cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
}
但它似乎无法在iOS7中运行。有什么建议?
答案 0 :(得分:12)
您需要递归搜索按钮。这应该是一种自动防故障方式:
- (void)viewDidLoad
{
[super viewDidLoad];
[self convertButtonTitle:@"Cancel" toTitle:@"Annuller" inView:self.searchBar];
}
- (void)convertButtonTitle:(NSString *)from toTitle:(NSString *)to inView:(UIView *)view
{
if ([view isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)view;
if ([[button titleForState:UIControlStateNormal] isEqualToString:from])
{
[button setTitle:to forState:UIControlStateNormal];
}
}
for (UIView *subview in view.subviews)
{
[self convertButtonTitle:from toTitle:to inView:subview];
}
}
我仅在iOS 7上对此进行了测试,但它也适用于iOS 6。
答案 1 :(得分:10)
只需为此代码执行: -
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
/* when user start editing in serchbar this method will display cancel button and disable the autocorrection functionality */
srcbar.showsCancelButton = YES;
for (UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
[cancelButton setTitle:@"hi" forState:UIControlStateNormal];
}
}
srcbar.autocorrectionType = UITextAutocorrectionTypeNo;
}
不在iOS7中测试,但在iOS6中这种工作正常,希望这对您有用。
输出为: -
答案 2 :(得分:10)
这是我对ios6和i的解决方案ios7
#define IS_IOS7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UISearchBar *searchBar = self.searchDisplayController.searchBar;
UIView *viewTop = IS_IOS7 ? searchBar.subviews[0] : searchBar;
NSString *classString = IS_IOS7 ? @"UINavigationButton" : @"UIButton";
for (UIView *subView in viewTop.subviews) {
if ([subView isKindOfClass:NSClassFromString(classString)]) {
UIButton *cancelButton = (UIButton*)subView;
[cancelButton setTitle:@"your title" forState:UIControlStateNormal];
}
}
}
答案 3 :(得分:8)
为了使其在iOS7上运行,您必须在搜索栏的子视图中搜索:
//iOS 7 hack
searchBar.showsCancelButton = YES;
UIView* view=searchBar.subviews[0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
[cancelButton setTitle:@"Anuluj" forState:UIControlStateNormal];
}
}
答案 4 :(得分:4)
适用于Swift 3.0
这很好。
func setSearchButtonText(text:String,searchBar:UISearchBar) {
for subview in searchBar.subviews {
for innerSubViews in subview.subviews {
if let cancelButton = innerSubViews as? UIButton {
cancelButton.setTitleColor(UIColor.white, for: .normal)
cancelButton.setTitle(text, for: .normal)
}
}
}
}
并调用方法
setSearchButtonText(text: "Done", searchBar: yourSearchBar)
这是输出
答案 5 :(得分:3)
将此行放在appDelegate
中
[[UIButton appearanceWhenContainedIn:[UISearchBar class],nil] setTitle:@“Your Title”forState:UIControlStateNormal];
答案 6 :(得分:0)
@Nitin Gohel是这样吗?
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIButton *cancelButton = nil;
for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
cancelButton = (UIButton*)subView;
[cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
}
}
}
答案 7 :(得分:0)
似乎“self.searchDisplayController.searchBar.subviews”只返回“searchBar”本身。我试着吼一声,它有效!
for (UIView *searBarView in [self.searchDisplayController.searchBar subviews])
{
for (UIView *subView in [searBarView subviews])
{
if ([subView isKindOfClass:[UIButton class]])
{
UIButton *cancleButton = (UIButton *)subView;
[cancleButton setTitle:@"hi" forState:UIControlStateNormal];
}
}
}
答案 8 :(得分:0)
在搜索开始之前,在UISearchBarDelagate或UISearchDisplayControllerDelegate中执行:
[self.searchDisplayController.searchBar.subviews enumerateObjectsUsingBlock:^(UIButton *subview, NSUInteger idx, BOOL *stop) {
if ([subview isKindOfClass:[UIButton class]]) {
[subview setTitle:@"OK" forState:UIControlStateNormal];
*stop = YES;
}
}];
答案 9 :(得分:0)
如果您只想将“取消”文本更改为您的语言环境中的相同,我认为正确的方法是使用本地化。方法如下:
en.lproj
目录(不确定是否有必要)编辑Info.plist
并添加以下行:
Localization native development region
:您所在的地区(例如:en
)Localizations
:例如English
注意:它在Simulator中不起作用,在设备上进行测试!
(资料来源:http://www.ibabbleon.com/iphone_app_localization.html)
答案 10 :(得分:0)
对于iOS7,这可行。
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIButton *cancelButton;
UIView *topView = self.searchDisplayController.searchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton) {
//Set the new title of the cancel button
[cancelButton setTitle:@"YourTitle" forState:UIControlStateNormal];
}
}
答案 11 :(得分:0)
参考上面的@ Salih解决方案,我使用了这段代码并且完美无缺!只需在app启动时调用AppDelegate.m中的方法setupAppearance即可。
- (void)setupAppearance
{
id appearance = nil;
if (IOS9_OR_LATER) {
appearance = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]];
} else {
appearance = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
}
[appearance setTitle:@"CANCEL"];
}
答案 12 :(得分:0)
使用UIKitUISearchResultsUpdating
协议方法updateSearchResults(for searchController: UISearchController)
中使用的代码进行访问:
if let cancelButton : UIButton = searchController.searchBar.value(forKey: "_cancelButton") as? UIButton {
cancelButton.setTitle("Back", for: UIControlState.normal)
}
(iOS 10,Swift 3.0)