iOS开发--UIKit控件之UISearchBar(搜索栏)

今天因为需求原因,需要用到搜索控件;之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者。

然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢谢!

原文地址:http://my.oschina.net/u/2340880/blog/509756

初始化:UISearchBar继承于UIView,我们可以像创建View那样创建searchBar

 UISearchBar *bar = [[UISearchBar alloc]initWithFrame:CGRectMake(, , , )];
 [self.view addSubview:bar];
 // 这个属性可以设置searchBar的搜索
 @property(nonatomic) UIBarStyle barStyle;
 // 枚举如下:
 typedef NS_ENUM(NSInteger, UIBarStyle) {
     UIBarStyleDefault          = ,//默认风格 白色搜索框,多出的背景为灰色
     UIBarStyleBlack            = ,//黑色风格,黑色的搜索框
     //下面两个枚举已经被禁用,作用和黑色风格一样
     UIBarStyleBlackOpaque      = , // Deprecated. Use UIBarStyleBlack
     UIBarStyleBlackTranslucent = , // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
 };

设置搜索框中的文字

 @property(nonatomic,copy) NSString *text; 

这个属性的官方解释是在搜索框顶部显示一行文字,其实就是背景文字,上图说明:

 @property(nonatomic,copy) NSString *prompt; 
 bar.prompt = @"搜索框";
 bar.text="

效果如下:

iOS开发--UIKit控件之UISearchBar(搜索栏)

和其他文本输入控件的placeholder相同,在输入文字时就会消失

 @property(nonatomic,copy) NSString *placeholder;  

是否在搜索框右侧显示一个图书的按钮,默认为NO,YES的效果如下:

 @property(nonatomic) BOOL showsBookmarkButton; 

iOS开发--UIKit控件之UISearchBar(搜索栏)

是否显示取消按钮,默认为NO,YES的效果如下:

 @property(nonatomic) BOOL showsCancelButton;

iOS开发--UIKit控件之UISearchBar(搜索栏)

是否显示搜索结果按钮,默认为NO,YES效果如下:

 @property(nonatomic) BOOL showsSearchResultsButton;

iOS开发--UIKit控件之UISearchBar(搜索栏)

设置搜索结果按钮的选中状态

 @property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected ;

设置显示取消按钮

 - (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated;

设置这个颜色值会影响搜索框中的光标的颜色

 @property(nonatomic,retain) UIColor *tintColor;

设置这个颜色会影响搜索框的背景颜色

 @property(nonatomic,retain) UIColor *barTintColor;

设置搜索框整体的风格

 @property (nonatomic) UISearchBarStyle searchBarStyle;

枚举如下:

 typedef NS_ENUM(NSUInteger, UISearchBarStyle) {
     UISearchBarStyleDefault,    // currently UISearchBarStyleProminent
     UISearchBarStyleProminent,  // 显示背景
     UISearchBarStyleMinimal     // 不显示背景
 } NS_ENUM_AVAILABLE_IOS(7_0);

设置是否半透明

 @property(nonatomic,assign,getter=isTranslucent) BOOL translucent;

是否显示搜索栏的附件选择按钮视图,要想显示这个视图,首先要将这个属性设置为YES,之后给按钮数组中添加按钮,使用下面这个属性:

 @property(nonatomic) BOOL showsScopeBar ;

设置选择按钮视图的按钮标题

 @property(nonatomic,copy) NSArray *scopeButtonTitles ;

 @property(nonatomic) NSInteger selectedScopeButtonIndex;

设置一个默认的选中按钮

bar = [[UISearchBar alloc]initWithFrame:CGRectMake(, , , )];
bar.showsScopeBar=YES;
bar.scopeButtonTitles = @["];

iOS开发--UIKit控件之UISearchBar(搜索栏)

键盘的附属视图

 @property (nonatomic, readwrite, retain) UIView *inputAccessoryView;

设置搜索框的背景图案

 @property(nonatomic,retain) UIImage *backgroundImage;

设置附属选择按钮视图的背景图案

 @property(nonatomic,retain) UIImage *scopeBarBackgroundImage;

这一对方法可以设置和获取某个状态枚举下的搜索框的背景图案

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics ;

- (UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics

这一对方法用于设置和获取搜索框中TextField的背景图案

- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state;

- (UIImage *)searchFieldBackgroundImageForState:(UIControlState)state;

这一对方法用于获取和设置搜索栏icon图片的图案

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

- (UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state ;

这一对方法用于设置和获取搜索框的附加选择按钮视图的背景图案

- (void)setScopeBarButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state;

- (UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state;

这一对方法用于获取和设置附加选择按钮视图中切换按钮的图案

- (void)setScopeBarButtonDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;

- (UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;

这一对方法用于设置和获取切换按钮标题文字的字体属性字典

- (void)setScopeBarButtonTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state;

- (NSDictionary *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state;

搜索文字在搜索框中的位置偏移

@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment;

textfield在搜索框中的位置偏移

@property(nonatomic) UIOffset searchTextPositionAdjustment;

设置搜索栏中图片的位置偏移

- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon;

- (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;

图片的枚举如下:

typedef NS_ENUM(NSInteger, UISearchBarIcon) {
    UISearchBarIconSearch, //搜索图标
    UISearchBarIconClear, // 清除图标
    UISearchBarIconBookmark, // 书本图标
    UISearchBarIconResultsList, // 结果列表图标
};

下面是搜索框控件的一些代理方法:

将要开始编辑时的回调,返回为NO,则不能编辑

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;

已经开始编辑时的回调

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;

将要结束编辑时的回调

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;

已经结束编辑的回调

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;

编辑文字改变的回调

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

编辑文字改变前的回调,返回NO则不能加入新的编辑文字

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text ;

搜索按钮点击的回调

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;

书本按钮点击的回调

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;

取消按钮点击的回调

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;

搜索结果按钮点击的回调

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;

搜索栏的附加视图中切换按钮触发的回调

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;

                                      2016-07-24

上一篇:JQUERY1.9学习笔记 之基本过滤器(十) 非选择器


下一篇:手把手教你Chrome扩展开发:本地存储篇