用户开始输入时更改搜索器图标
Posted
技术标签:
【中文标题】用户开始输入时更改搜索器图标【英文标题】:Changing Searcher Icon when user start typing 【发布时间】:2016-04-13 11:43:06 【问题描述】:当用户开始输入时,我正在尝试将 SearchBar 搜索图标更改为后退按钮,例如谷歌地图。
我使用了以下代码
[[[UISearchBar class] appearance] setImage:[UIImage imageNamed:@"icon_me"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
但这会将 Icon 设置在开始本身,而不是在我们开始输入时 我只需要在 viewDidLoad 中写下这一行。
请帮助我。
【问题讨论】:
【参考方案1】:你需要实现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
【讨论】:
感谢您的回复。我已经尝试过了。但这对我不起作用以上是关于用户开始输入时更改搜索器图标的主要内容,如果未能解决你的问题,请参考以下文章