当用户开始input时更改search器图标

我试图改变search栏search图标后退button,如谷歌地图,当用户开始input。

我用下面的代码

[[[UISearchBar class] appearance] setImage:[UIImage imageNamed:@"icon_me"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

但是,这将设置图标在开始自己,而不是当我们开始打字,我不得不写在viewDidLoad这一行。

请帮帮我。

你需要实现UISearchBar委托方法。

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; // called when text changes (including clear) - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; 

在委托方法中,您需要执行此操作

 UITextField *textField = [searchBar valueForKey:@"_searchField"]; textField.textColor = [UIColor brownColor]; //you can set text color textField.leftViewMode = UITextFieldViewModeWhileEditing; //set leftViewMode or you can also set rightViewMode also UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)]; imgview.image = [UIImage imageNamed:@"searchIcon.png"]; //you need to set search icon for textfield's left/right view as you want textField.leftView = imgview; //textField.rightView = imgview; //you can set at right side also