码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-07-24 11:49:33      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

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

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

设置搜索框中的文字

1 @property(nonatomic,copy) NSString *text; 

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

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

效果如下:

技术分享

 

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

1 @property(nonatomic,copy) NSString *placeholder;  

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

1 @property(nonatomic) BOOL showsBookmarkButton; 

技术分享

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

1 @property(nonatomic) BOOL showsCancelButton;

技术分享

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

1 @property(nonatomic) BOOL showsSearchResultsButton;

技术分享

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

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

设置显示取消按钮

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

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

1 @property(nonatomic,retain) UIColor *tintColor;

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

1 @property(nonatomic,retain) UIColor *barTintColor;

设置搜索框整体的风格

1 @property (nonatomic) UISearchBarStyle searchBarStyle;

枚举如下:

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

 

设置是否半透明

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

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

1 @property(nonatomic) BOOL showsScopeBar ;

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

1 @property(nonatomic,copy) NSArray *scopeButtonTitles ;
2 
3 @property(nonatomic) NSInteger selectedScopeButtonIndex;

设置一个默认的选中按钮

bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 200)];
bar.showsScopeBar=YES;
bar.scopeButtonTitles = @[@"12",@"2",@"3",@"4"];

技术分享

键盘的附属视图

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

设置搜索框的背景图案

1 @property(nonatomic,retain) UIImage *backgroundImage;

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

1 @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;

 

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

标签:

原文地址:http://www.cnblogs.com/Zy-iOS-GS/p/5700261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!