searchBar

_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];

pragma mark - UISearchBarDelegate 协议

// UISearchBar得到焦点并开始编辑时,执行该方法

  • (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    [self.searchBar setShowsCancelButton:YES animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self controlAccessoryView:0.9];// 显示遮盖层。
    return YES;

}

// 取消按钮被按下时,执行的方法

  • (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
    [self.searchBar setShowsCancelButton:NO animated:YES];
    [liveViewAreaTable searchDataBySearchString:nil];// 搜索tableView数据
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self controlAccessoryView:0];// 隐藏遮盖层。

}

// 键盘中,搜索按钮被按下,执行的方法

  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"---%@",searchBar.text);
    [self.searchBar resignFirstResponder];// 放弃第一响应者
    [liveViewAreaTable searchDataBySearchString:searchBar.text];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self controlAccessoryView:0];// 隐藏遮盖层。

}

// 当搜索内容变化时,执行该方法。很有用,可以实现时实搜索

  • (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{
    NSLog(@"textDidChange---%@",searchBar.text);
    [liveViewAreaTable searchDataBySearchString:searchBar.text];// 搜索tableView数据
    [self controlAccessoryView:0];// 隐藏遮盖层。

}

3.遍历UISearchBar控件的子控件,这样可以针对不同的子视图来设置外观了。

for(id subView in [self.searchBar subviews]){
if([subView isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton *)subView;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}

你可能感兴趣的:(searchBar)