UISearchBar

1、UISearchBar的基本属性

// 初始化
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];

    [self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符
    [self.searchBar setPrompt:@"Prompt"];// 顶部提示文本,相当于控件的Title
    [self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框样式
    [self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的颜色,当设置此属性时,barStyle将失效
    [self.searchBar setTranslucent:YES];// 设置是否透明
    [self.searchBar setBackgroundImage:[UIImage imageNamed:@"image0"]];// 设置背景图片
    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"] forState:UIControlStateNormal];// 设置搜索框中文本框的背景
    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image0"] forState:UIControlStateHighlighted];
    [self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30, 30)];// 设置搜索框中文本框的背景的偏移量

    [self.searchBar setSearchResultsButtonSelected:NO];// 设置搜索结果按钮是否选中
    [self.searchBar setShowsSearchResultsButton:YES];// 是否显示搜索结果按钮

    [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30, 0)];// 设置搜索框中文本框的文本偏移量

    [self.searchBar setInputAccessoryView:_btnHide];// 提供一个遮盖视图
    [self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 设置键盘样式

    // 设置搜索框下边的分栏条
    [self.searchBar setShowsScopeBar:YES];// 是否显示分栏条
    [self.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分栏条,栏目
    [self.searchBar setScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];// 分栏条的背景颜色
    [self.searchBar setSelectedScopeButtonIndex:1];// 分栏条默认选中的按钮的下标


    [self.searchBar setShowsBookmarkButton:YES];// 是否显示右侧的“书图标”

    [self.searchBar setShowsCancelButton:YES];// 是否显示取消按钮
    [self.searchBar setShowsCancelButton:YES animated:YES];

    // 是否提供自动修正功能(这个方法一般都不用的)
    [self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 设置自动检查的类型
    [self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自动修正功能,一般设置为UITextAutocorrectionTypeDefault

    self.searchBar.delegate = self;// 设置代理
    [self.searchBar sizeToFit];
    myTableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);


    [self.view addSubview:myTableView];

    [myTableView addSubview:self.searchBar];

2.代理方法

//搜索框中的内容发生改变时 回调(即要搜索的内容改变)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 
    NSLog(@"changed"); 
    if (_searchBar.text.length == 0) { 
        [self setSearchControllerHidden:YES]; //控制下拉列表的隐现
    }else{ 
        [self setSearchControllerHidden:NO]; 

    } 
} 

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { 
    searchBar.showsCancelButton = YES; 
for(id cc in [searchBar subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)cc;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
    NSLog(@"shuould begin"); 
    return YES; 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    searchBar.text = @""; 
    NSLog(@"did begin"); 
} 

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { 
    NSLog(@"did end"); 
    searchBar.showsCancelButton = NO; 

} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"search clicked"); 
} 

//点击搜索框上的 取消按钮时 调用
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"cancle clicked"); 
    _searchBar.text = @""; 
    [_searchBar resignFirstResponder]; 
    [self setSearchControllerHidden:YES]; 
} 

你可能感兴趣的:(searchBar)