在开发中经常会用到搜索功能,但系统的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];
}```
网友评论