UISearchBar

iOS中UISearchBar(搜索框)使用总结

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

?
1
2
     UISearchBar * bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 40)];
     [self.view addSubview:bar];

@property(nonatomic)        UIBarStyle              barStyle; 

这个属性可以设置searchBar的搜索框的风格,枚举如下:

?
1
2
3
4
5
6
7
typedef  NS_ENUM(NSInteger, UIBarStyle) {
     UIBarStyleDefault          = 0, //默认风格 白色搜索框,多出的背景为灰色
     UIBarStyleBlack            = 1, //黑色风格,黑色的搜索框
     //下面两个枚举已经被禁用,作用和黑色风格一样
     UIBarStyleBlackOpaque      = 1,  // Deprecated. Use UIBarStyleBlack
     UIBarStyleBlackTranslucent = 2,  // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
};

@property(nonatomic,copy)   NSString               *text; 

设置搜索框中的文字

@property(nonatomic,copy)   NSString               *prompt; 

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

?
1
2
    bar.prompt = @ "搜索框" ;
    bar.text=@ "321111111111111111111111111"

效果如下:

@property(nonatomic,copy)   NSString               *placeholder;  

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

@property(nonatomic)        BOOL                    showsBookmarkButton; 

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

@property(nonatomic)        BOOL                    showsCancelButton;

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

@property(nonatomic)        BOOL                    showsSearchResultsButton;

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

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

设置搜索框整体的风格,枚举如下:

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

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

设置是否半透明

@property(nonatomic)      BOOL       showsScopeBar ;

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

@property(nonatomic,copy) NSArray   *scopeButtonTitles ;

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

@property(nonatomic)      NSInteger  selectedScopeButtonIndex;

设置一个默认的选中按钮

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

@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

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


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

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

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


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

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

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

- (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;

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

@property(nonatomic) UIOffset searchTextPositionAdjustment;

textfield在搜索框中的位置偏移


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

- (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;

设置搜索栏中图片的位置偏移,图片的枚举如下:

?
1
2
3
4
5
6
typedef  NS_ENUM(NSInteger, UISearchBarIcon) {
     UISearchBarIconSearch,  //搜索图标
     UISearchBarIconClear,  // 清除图标
     UISearchBarIconBookmark,  // 书本图标
     UISearchBarIconResultsList,  // 结果列表图标
};


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

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

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

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

已经开始编辑时的回调

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

将要结束编辑时的回调

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

已经结束编辑的回调

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   编辑文字改变的回调

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

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

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

搜索按钮点击的回调

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

书本按钮点击的回调

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

取消按钮点击的回调

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

搜索结果按钮点击的回调


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

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


UISearchBar 修改背景颜色透明

输入相关关键字在百度中能搜索出来很多,但是都不靠谱,于是Google了一下,在StackOverFlow中找到一个蛮靠谱的方法,试了可行。

 

with iOS8 sdks apple moved @"UISearchBarBackground" view one level deeper, so have will need to look at subviews of the child-views as bellow,.


for (UIView *subview in searchBar.subviews) {
        for(UIView* grandSonView in subview.subviews){
            if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                grandSonView.alpha = 0.0f;
            }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
                NSLog(@"Keep textfiedld bkg color");
            }else{
                grandSonView.alpha = 0.0f;
            }
        }//for cacheViews
    }//subviews

耶!

 

接下来,我又发现一位博主分享的文章,写的很详细,而且解决方案看起来会更加优雅,不像上面给的那样暴力粗鲁。^_^

文章在这里

这里贴出代码:

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1
    UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:32.0f];
    // 2
    [_searchBar setBackgroundImage:clearImg];
    // 3
    [_searchBar setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
    // 4
    [_searchBar setBackgroundColor:[UIColor clearColor]];
}

+ (UIImage*) imageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
复制代码

修改APP内全局UISearchBar样式:

复制代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIColor* myColor = [UIColor colorWithRed:0 green:0.48 blue:1 alpha:1]; // set your desired background color here

    UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:1.0f];
    UIImage* coloredImg = [CDViewController imageWithColor:myColor andHeight:32.0f];

    [[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
    [[UISearchBar appearance] setBackgroundImage:clearImg];
    [[UISearchBar appearance] setSearchFieldBackgroundImage:coloredImg forState:UIControlStateNormal];

    return YES;
}
复制代码

给ScopeBar设置透明背景:

复制代码
UIImage* clearImg = [CDViewController imageWithColor:[UIColor clearColor] andHeight:1.0f];

[_searchBar setScopeButtonTitles:nil];
[_searchBar setScopeBarBackgroundImage:clearImg];
[_searchBar setScopeBarButtonBackgroundImage:clearImg forState:UIControlStateNormal];
[_searchBar setScopeBarButtonDividerImage:clearImg forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
复制代码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值