微博(四)封装SearchBar

封装SearchBar

+(instancetype)searchBar;

实现方法

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

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // 背景
    self.background = [UIImage resizedImageWithName:@"searchbar_textfield_background"];
    // 左边的放大镜图标
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"searchbar_textfield_search_icon"]];
    iconView.contentMode = UIViewContentModeCenter;
    self.leftView = iconView;
    self.leftViewMode = UITextFieldViewModeAlways;
    // 字体
    self.font = [UIFont systemFontOfSize:13];
    // 右边的清除按钮
    self.clearButtonMode = UITextFieldViewModeAlways;
    // 设置提醒文字
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索" attributes:attrs];
    // 设置键盘右下角按钮的样式
    self.returnKeyType = UIReturnKeySearch;
    self.enablesReturnKeyAutomatically = YES;
}
return self;
}

-(void)layoutSubviews
{
[super layoutSubviews];
// 设置左边图标的frame
self.leftView.frame = CGRectMake(0, 0, 30, self.frame.size.height);
}
  • 调用方法

SXWSearchBar *searchBar = [SXWSearchBar searchBar]; searchBar.frame = CGRectMake(0, 0, 300, 40); self.navigationItem.titleView = searchBar;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,705评论 2 7
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,583评论 2 4
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,693评论 0 3
  • 爸爸,在我的心里特别的具象。爸爸很乐观,遇到再难的事情,也从不在我们面前抱怨。爸爸很温柔,小时候我留着一头长发,由...
    芥末凉皮阅读 272评论 0 0
  • 非常荣幸,我是第33位,进了班长办公室群。群主叫帐幔内的敬拜,而且名字前面挂了一个铃铛,名字后面加了合掌的图标...
    石三英语阅读 578评论 1 3