美文网首页
iOS开发之使用UITextFiled自定义searchBar

iOS开发之使用UITextFiled自定义searchBar

作者: LearningCoding | 来源:发表于2016-06-02 10:38 被阅读311次

在开发中经常会用到搜索功能,但系统的searchBar很难满足UI的要求,就需要自定义searchBar了,直接上代码

#import <UIKit/UIKit.h>

@interface WXSearchBar : UITextField

+ (instancetype)searchBarWithFrame:(CGRect)frame;

@end```

import "WXSearchBar.h"

@implementation WXSearchBar

  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];

    if (self) {
    self.font = [UIFont systemFontOfSize:15];
    self.placeholder = @"请输入查询条件";
    self.background = [UIImage imageNamed:@""];

      UIImageView *searchIcon = [[UIImageView alloc] init];
      searchIcon.frame = CGRectMake(0, 0, 30, 30);
      searchIcon.contentMode = UIViewContentModeCenter;
      searchIcon.image = [UIImage imageNamed:@""];
      
      self.leftView = searchIcon;
      self.leftViewMode = UITextFieldViewModeAlways;
    

    }

    return self;

}

  • (instancetype)searchBarWithFrame:(CGRect)frame
    {
    return [[self alloc] initWithFrame:frame];
    }```

相关文章

网友评论

      本文标题:iOS开发之使用UITextFiled自定义searchBar

      本文链接:https://www.haomeiwen.com/subject/pgxkdttx.html