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
//
// SelectStoreHeadView.m
// redstar
//
// Created by admin on 15/12/16.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import "SelectStoreHeadView.h"
@implementation SelectStoreHeadView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.searchTextFiled.placeholder = @"请输入问题关键字";
self.layer.borderColor = kSeparateLineCGColor;
self.layer.borderWidth = 0.5;
}
return self;
}
#pragma mark - Lazy Loading
- (UITextField *)searchTextFiled
{
if (!_searchTextFiled) {
_searchTextFiled = [[UITextField alloc] init];
_searchTextFiled.font = [UIFont systemFontOfSize:14.0];
_searchTextFiled.backgroundColor = [UIColor whiteColor];
_searchTextFiled.translatesAutoresizingMaskIntoConstraints = NO;
_searchTextFiled.layer.cornerRadius = 4.0;
_searchTextFiled.layer.borderWidth = 1.0;
_searchTextFiled.layer.borderColor = kPasswordBorderColor;
UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_icon"]];
imageV.width += 20;
imageV.contentMode = UIViewContentModeCenter;
_searchTextFiled.leftView = imageV;
_searchTextFiled.leftViewMode = UITextFieldViewModeAlways;
UIButton *imageR = [[UIButton alloc] init];
imageR.frame = CGRectMake(0, 0, 28, 28);
[imageR setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
[imageR addTarget:self action:@selector(searchClick:) forControlEvents:UIControlEventTouchUpInside];
imageR.contentMode = UIViewContentModeCenter;
imageR.width += 10;
_searchTextFiled.rightView = imageR;
_searchTextFiled.rightViewMode = UITextFieldViewModeAlways;
[self addSubview:_searchTextFiled];
// 顶端
NSLayoutConstraint *titleTop = [NSLayoutConstraint constraintWithItem:_searchTextFiled attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:8];
[self addConstraint:titleTop];
// 左边
NSLayoutConstraint *titleLeft = [NSLayoutConstraint constraintWithItem:_searchTextFiled attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
[self addConstraint:titleLeft];
// 右边
NSLayoutConstraint *titleRight = [NSLayoutConstraint constraintWithItem:_searchTextFiled attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self addConstraint:titleRight];
// 高度
NSLayoutConstraint *titleBottom = [NSLayoutConstraint constraintWithItem:_searchTextFiled attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-8];
[self addConstraint:titleBottom];
}
return _searchTextFiled;
}
- (void)searchClick:(UIButton *)sender
{
self.searchTextFiled.text = @"";
}
@end