UISearchBar 搜索条

2020-04-09  本文已影响0人  邻家小哥哥

属性

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 50, 100, 60)];
searchBar.barStyle = UISearchBarStyleDefault;

UISearchBarStyleProminent 用我的邮件、消息和联系人
UISearchBarStyleMinimal 使用的日历,笔记和音乐

searchBar.prompt = @"提示";
searchBar.prompt = @"提示";
searchBar.placeholder = @"请输入";
searchBar.showsBookmarkButton = YES;
searchBar.showsCancelButton = YES;
searchBar.showsSearchResultsButton = YES;
searchBar.searchResultsButtonSelected = YES;
searchBar.tintColor = [UIColor redColor];
searchBar.translucent = YES;
searchBar.showsScopeBar = YES;
searchBar.selectedScopeButtonIndex = 1;
textfield.autocapitalizationType = UITextAutocapitalizationTypeNone; //不自动大写

UITextAutocapitalizationTypeWords 单词首字母大写
UITextAutocapitalizationTypeSentences 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters 所有字母都大写

textfield.autocorrectionType = UITextAutocorrectionTypeDefault;

UITextAutocorrectionTypeNo 不自动纠错

UITextAutocorrectionTypeYes 自动纠错

searchBar.backgroundImage = [UIImage imageNamed:@"1.png"];
searchBar.scopeBarBackgroundImage = [UIImage imageNamed:@"2.png"];

代理

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ // called when text starts editing
[searchBar setShowsCancelButton:YES animated:YES]; // 动画显示取消按钮
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; // return NO to not resign first responder
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; // called when text ends editing
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ // called when text changes (including clear)

按钮点击方法

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; // called when bookmark button pressed
-(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{ // called when cancel button pressed
[searchBar setShowsCancelButton:NO animated:NO]; // 取消按钮回收
[searchBar resignFirstResponder]; // 取消第一响应值,键盘回收,搜索结束
}
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBarNS_AVAILABLE_IOS(3_2);// called when search results button pressed
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScopeNS_AVAILABLE_IOS(3_0);
上一篇下一篇

猜你喜欢

热点阅读