1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#import "CustomSearchBar.h"
@interface CustomSearchBar ()
@end
@implementation CustomSearchBar
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self addSubview:self.bgView]; [self addSubview:self.textField]; } return self; }
+(instancetype)searchBar { return [[self alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)]; }
- (UITextField *)textField { if(_textField == nil) { _textField = [[UITextField alloc] init]; _textField.font = [UIFont systemFontOfSize:15]; _textField.placeholder = @"搜索";
UIImageView *searchIcon = [[UIImageView alloc] init]; searchIcon.image = [UIImage imageNamed:@"search_search_icon"]; searchIcon.contentMode = UIViewContentModeCenter; CGRect frame = CGRectZero; frame.size = CGSizeMake(30, 30); searchIcon.frame =frame; _textField.leftView = searchIcon; _textField.leftViewMode = UITextFieldViewModeAlways; _textField.frame=CGRectMake(5, 5,self.bounds.size.width-10, self.bounds.size.height-10); _textField.backgroundColor=[UIColor whiteColor]; _textField.layer.borderColor=RGBCOLOR(200, 200, 200).CGColor; _textField.layer.borderWidth=1; _textField.layer.cornerRadius = (self.bounds.size.height-10)/2; _textField.layer.masksToBounds = YES; } return _textField; }
- (UIView *)bgView { if(_bgView == nil) { _bgView = [[UIView alloc] initWithFrame:self.bounds]; _bgView.backgroundColor =RGBCOLOR(225, 227, 234); } return _bgView; }
@end
|