UISearchBar

创建

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:frame];

样式(区别如图所示)

    @property (nonatomic) UISearchBarStyle searchBarStyle

    searchBar.searchBarStyle = UISearchBarStyleDefault;  // currently UISearchBarStyleProminent
                           UISearchBarStyleMinimal;  // used my Mail, Messages and Contacts 不显示背景
                           UISearchBarStyleProminent  // used by Calendar, Notes and Music 显示背景

搜索框样式(区别如图所示)

    @property(nonatomic)  UIBarStyle barStyle

    searchBar.barStyle = UIBarStyleDefault;         = 0,
                     UIBarStyleBlack            = 1,
                     UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack(弃用,和black一样)
                     UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES (弃用,和black一样)

text

    @property(nullable,nonatomic,copy)   NSString               *text;                  // current/starting search text

    searchBar.text = @"UISearchBar";

placeholder

    @property(nullable,nonatomic,copy)   NSString               *placeholder;           // default is nil

    searchBar.placeholder = @"Placeholder";

textField和textField的输入框的偏移

    //  textField的偏移
    searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(<#CGFloat horizontal#>, <#CGFloat vertical#>);
    //  输入框的位置偏移
    searchBar.searchTextPositionAdjustment = UIOffsetMake(<#CGFloat horizontal#>, <#CGFloat vertical#>);
    UIOffsetMake(<#CGFloat horizontal#>, <#CGFloat vertical#>);  //  负左正右,负上正下

辅助按钮

    //  bookmarkButton和searchResultsButton只能存在一个
    @property(nonatomic) BOOL showsBookmarkButton        // default is NO
    @property(nonatomic) BOOL showsCancelButton          // default is NO
    @property(nonatomic) BOOL showsSearchResultsButton   // default is NO,显示搜索结果按钮
    @property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected  // default is NO,是否显示搜索结果按钮

    //  对应方法(代理)
    - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;     // called when bookmark button pressed
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;    // called when cancel button pressed
    - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;   // called when search results button pressed
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;   // called when keyboard search button pressed

tintColor && barTintColor

    @property(null_resettable, nonatomic,strong) UIColor *tintColor;       //  光标和取消按钮一起变色
    @property(nullable, nonatomic,strong) UIColor *barTintColor    // default is nil,是前背景色
    //  如果是设置background会被前背景色挡住

translucent

    @property(nonatomic,assign,getter=isTranslucent) BOOL translucent  //  是否设置半透明

附属选项视图

    //  1、设置显示
    searchBar.showsScopeBar = YES;
    //  2、选项的标题
    searchBar.scopeButtonTitles = [NSMutableArray arrayWithObjects:@"One", @"Two", @"Three",nil];
    //  3、默认选择的index
    searchBar.selectedScopeButtonIndex = 1;

修改默认小图标

    - (void)setImage:(nullable UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state {};

    typedef NS_ENUM(NSInteger, UISearchBarIcon) {
    UISearchBarIconSearch, // 放大镜
    UISearchBarIconClear , // 
    UISearchBarIconBookmark , // The open book icon
    UISearchBarIconResultsList, // 搜索结果按钮
    };

键盘的附属视图如图:

    @property (nullable, nonatomic, readwrite, strong) UIView *inputAccessoryView;

    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];  //  x,y,width自动设置,height才会根据设置的数值起作用,height = 0就没有附属视图了
    redView.backgroundColor = [UIColor redColor];
    searchBar.inputAccessoryView = redView;

搜索框的背景如图:

    @property(nullable, nonatomic,strong) UIImage *backgroundImage;

    UIImage *redImage = [UIImage imageNamed:@"1.png"];
    searchBar.backgroundImage = redImage;

附属选项视图如图:

    @property(nullable, nonatomic,strong) UIImage *scopeBarBackgroundImage;

    UIImage *image = [UIImage imageNamed:@"1.png"];
    searchBar.scopeBarBackgroundImage = image;

UISearchBarDelegate

    //  编辑状态回调
    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;                      // return NO to not become first responder
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;                     // called when text starts editing
    - (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)
    - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0); // called before text changes

    //  辅助按钮触发事件
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;                     // called when keyboard search button pressed
    - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED; // called when bookmark button pressed
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED;   // called when cancel button pressed
    - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; // called when search results button pressed

    //  选择附属选项触发事件,当点击当前选项时不会触发此事件
    - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);

获取到search.textField(UITextField细节设置查看笔记1.09)

    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];

results matching ""

    No results matching ""