UI - UISearchBar

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate>
@property (nonatomic, strong) UISearchBar *searchBar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 100, 320, 80)];
    _searchBar.delegate = self;
    [self.view addSubview:_searchBar];

    [self settingGeneralProperty];
    [self settingSearchFieldInSearchBar];
    [self settingSearchBarIconInSearchFieldRight];
    [self settingScopeBarInBottom];
    
}

//常规属性
- (void)settingGeneralProperty
{
    /**
     *  bar 风格
     UIBarStyleDefault          = 0,
     UIBarStyleBlack            = 1,
     */
    _searchBar.barStyle = UIBarStyleDefault;
    
    /**
     *  searchbar 风格
     UISearchBarStyleDefault,    // currently UISearchBarStyleProminent
     UISearchBarStyleProminent,  // used my Mail, Messages and Contacts(provides no default background color or image but will display one if customized as such系统提供的颜色和图片无效,自定制有效)
     UISearchBarStyleMinimal
     */
    _searchBar.searchBarStyle = UISearchBarStyleDefault;
    
    // 指定控件是否会有透视效果
    _searchBar.translucent =YES;
    // 光标颜色,cancel 按钮,底部选择栏等得 tintcolor
    _searchBar.tintColor = [UIColor redColor];
    // bar 的颜色
    _searchBar.barTintColor = [UIColor cyanColor];
    // 设置控件背景图片
    _searchBar.backgroundImage = [UIImage imageNamed:@"bg_1.png"];
    // Use UIBarMetricsDefaultPrompt to set a separate backgroundImage for a search bar with a prompt
    //    [_searchBar setBackgroundImage:<#(nullable UIImage *)#> forBarPosition:<#(UIBarPosition)#> barMetrics:<#(UIBarMetrics)#>];
}
//设置搜索输入框,类似于 textfield
- (void)settingSearchFieldInSearchBar
{
    // 顶部提示文字,类似标题, 默认 nil
    _searchBar.prompt = @"prompt";
    // text, 默认 nil
    //    _searchBar.text = @"text";
    // 占位符, 默认 nil
    _searchBar.placeholder = @"请输入要搜索的词语";
    //输入框背景图片设置
    [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"bg_3.png"] forState:UIControlStateNormal];
    //获取输入框背景图片
    [_searchBar searchFieldBackgroundImageForState:UIControlStateNormal];
    // 键盘的样式
    _searchBar.keyboardType =UIKeyboardTypeNumberPad;
    /* 设置在什么的情况下自动大写
     UITextAutocapitalizationTypeNone,             //除非自己点击大写,否则永不大写
     UITextAutocapitalizationTypeWords,            //以单词来区分,每个单词首字母大写
     UITextAutocapitalizationTypeSentences,        //以句子来区分
     UITextAutocapitalizationTypeAllCharacters,    //所有字母全部大写
     */
    _searchBar.autocapitalizationType =UITextAutocapitalizationTypeNone;
    //自定义键辅助视图
    _searchBar.inputAccessoryView = [[UIView alloc] init];
    //获取辅助视图栏的项目, iOS 新特性,只读,获取到以后可以认为添加处理
    _searchBar.inputAssistantItem;
}
//设置搜索栏右边按钮图
- (void)settingSearchBarIconInSearchFieldRight
{
    // 是否显示cancel按钮(静态)
    //    _searchBar.showsCancelButton = YES;
    [_searchBar setShowsCancelButton:YES animated:YES];
    // 右端按钮是否为Bookmark, 默认 NO
    _searchBar.showsBookmarkButton =YES;
    // 右端按钮是否为搜索结果按钮,与书的按钮位置相同冲突
    _searchBar.showsSearchResultsButton =YES;
    // 搜索结果按钮是否被选中
    _searchBar.searchResultsButtonSelected =YES;
    // 右端按钮自定义图片
    /**
     *    SearchBarIcon风格
     UISearchBarIconSearch,         // The magnifying glass
     UISearchBarIconClear,          // The circle with an x in it
     UISearchBarIconBookmark,       // The open book icon
     UISearchBarIconResultsList,    // The list lozenge icon
     */
    [_searchBar setImage:[UIImage imageNamed:@"qq.png"]forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];
    //获取某风格某状态下右端按钮图片
    [_searchBar imageForSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];
    //设置右端按钮的位置
    //    [_searchBar setPositionAdjustment:<#(UIOffset)#> forSearchBarIcon:<#(UISearchBarIcon)#>]
}
//搜索栏下部选择栏
- (void)settingScopeBarInBottom
{
    // 搜索栏下部的选择栏,出入字符串数组形成对应的 segment 控件
    _searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"iOS",@"Android",@"Windows",nil];
    // 默认选择栏按钮的索引
    _searchBar.selectedScopeButtonIndex = 2;
    // 当显示下部选择栏时,是否自动适配控件高度,已防止控件给定高度小于80显示不全时
    _searchBar.showsScopeBar =YES;
    // 设置搜索栏下部背景图片
    _searchBar.scopeBarBackgroundImage = [UIImage imageNamed:@"bg_2.png"];
    
    //    [_searchBar setScopeBarButtonBackgroundImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>]
    //    [_searchBar setScopeBarButtonDividerImage:<#(nullable UIImage *)#> forLeftSegmentState:<#(UIControlState)#> rightSegmentState:<#(UIControlState)#>]
    //    [_searchBar setScopeBarButtonTitleTextAttributes:<#(nullable NSDictionary<NSString *,id> *)#> forState:<#(UIControlState)#>]
}

#pragma mark - UISearchBarDelegate

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"是否开始编辑");
    return YES;
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"text 已经开始编辑");
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"是否结束编辑");
    return YES;
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"text 已经结束编辑");
}
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"是否允许输入当前字符,当前字符位置:%ld-%ld,当前字符:%@",range.location,range.length,text);
    return YES;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"输入文字已经改变");
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"点击了取消按钮");
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"点击了 search 按钮");
}
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"点击了 bookmark 按钮");
}
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"点击了 resultslist 按钮");
}
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    NSLog(@"底部选择栏切到%ld",selectedScope);
}


//结束编辑
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end

你可能感兴趣的:(总结,搜索,UISearchBar,搜索栏)