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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// SearchToolBar.m
// redstar
//
// Created by admin on 15/11/10.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import "SearchToolBar.h"
@implementation SearchToolBar
#pragma mark - init Methods
- (instancetype)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
#pragma mark - Private Methods
- (void)setup
{
self.layer.borderColor = kPasswordBorderColor;
self.layer.borderWidth = 1.0;
self.searchTextFiled.placeholder = @"请输入问题关键字";
[self.queryButton setTitle:@"快速查询" forState:UIControlStateNormal];
}
#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;
[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.queryButton attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-10];
[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;
}
- (UIButton *)queryButton
{
if (!_queryButton) {
_queryButton = [[UIButton alloc] init];
_queryButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
_queryButton.layer.cornerRadius = 4.0;
_queryButton.backgroundColor = kNavigationBarColor;
_queryButton.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_queryButton];
// 顶端
NSLayoutConstraint *titleTop = [NSLayoutConstraint constraintWithItem:_queryButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:9];
[self addConstraint:titleTop];
// 右边
NSLayoutConstraint *titleRight = [NSLayoutConstraint constraintWithItem:_queryButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self addConstraint:titleRight];
// 高度
NSLayoutConstraint *titleBottom = [NSLayoutConstraint constraintWithItem:_queryButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-9];
[self addConstraint:titleBottom];
// 宽度
NSLayoutConstraint *titleWidth = [NSLayoutConstraint constraintWithItem:_queryButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80];
[self addConstraint:titleWidth];
}
return _queryButton;
}
@end