使用UITextField自定义UISearchBar

有的时候,使用iOS自带的控件不符合变态产品狗的需求时,我们只好自定义控件来满足他们。在此处,我们通过创建一个GUSearchBar类,继承自UITextField类来自定义搜索框。

使用UITextField自定义UISearchBar_第1张图片

创建GUSearchBar

代码如下:GUSearchBar.h

#import 
@interface GUSearchBar : UITextField

+(instancetype) searchBar;

@end

GUSearchBar.m

#import "GUSearchBar.h"

@implementation GUSearchBar

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.background = [UIImage resizedImage:@"searchbar_textfield_background"];
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        UIImageView *leftView = [[UIImageView alloc] init];
        leftView.image = [UIImage imageWithName:@"searchbar_textfield_search_icon"];

        leftView.width = leftView.image.size.width + 10;
        leftView.height = leftView.image.size.height;
        leftView.contentMode = UIViewContentModeCenter;
        
        self.leftView = leftView;
        self.leftViewMode = UITextFieldViewModeAlways;
        self.clearButtonMode = UITextFieldViewModeAlways;
        self.placeholder = @"搜索";
    }
    return self;
}

+(instancetype) searchBar
{
    return [[self alloc] init];
}

@end

注意:上述有类似leftView.width,leftView.height这样的语法是使用了自定义UIView的扩展分类,详情请点击UIView扩展。

使用

GUSearchBar *searchBar = [[GUSearchBar alloc] init];
searchBar.width = 350;
searchBar.height = 30;
    
self.navigationItem.titleView = searchBar;

使用效果

使用UITextField自定义UISearchBar_第2张图片
GUSearchBar

来自RecluseGu的博客:http://reclusegu.github.io/2016/06/29/Customizing-UISearchBar-with-UITextField/

你可能感兴趣的:(使用UITextField自定义UISearchBar)