forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategorySearchBar.m
More file actions
109 lines (92 loc) · 3.52 KB
/
CategorySearchBar.m
File metadata and controls
109 lines (92 loc) · 3.52 KB
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
//
// CategorySearchBar.m
// Coding_iOS
//
// Created by jwill on 15/11/18.
// Copyright © 2015年 Coding. All rights reserved.
//
#import "CategorySearchBar.h"
@interface CategorySearchBar ()
@property (copy,nonatomic)SelectBlock curBlock;
@property (strong, nonatomic)UIButton *categoryBtn;
@property (strong, nonatomic)UIButton *iconBtn;
@end
@implementation CategorySearchBar
-(void)layoutSubviews
{
self.autoresizesSubviews = YES;
NSPredicate *finalPredicate = [NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary *bindings) {
if ([candidateView isMemberOfClass:NSClassFromString(@"UISearchBarTextField")]) {
return true;
}else{
return false;
}
}];
//找到输入框 右移
UITextField *searchField=[[[[[self subviews] firstObject] subviews] filteredArrayUsingPredicate:finalPredicate] lastObject];
searchField.textAlignment=NSTextAlignmentLeft;
[searchField setFrame:CGRectMake(53,4.8,self.frame.size.width-55,22)];
//
[(UIImageView*)searchField.leftView setFrame:CGRectZero];
}
-(void)patchWithCategoryWithSelectBlock:(SelectBlock)block{
[self addSubview:self.categoryBtn];
[self addSubview:self.iconBtn];
_curBlock=block;
}
-(UIButton*)categoryBtn{
if (!_categoryBtn) {
_categoryBtn=[UIButton new];
_categoryBtn.frame=CGRectMake(5, 0, 40, 31);
[_categoryBtn addTarget:self action:@selector(selectCategoryAction) forControlEvents:UIControlEventTouchUpInside];
_categoryBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[_categoryBtn setTitleColor:[UIColor colorWithHexString:@"0x666666"] forState:UIControlStateNormal];
[_categoryBtn setTitle:@"项目" forState:UIControlStateNormal];
}
return _categoryBtn;
}
-(UIButton*)iconBtn{
if (!_iconBtn) {
_iconBtn=[[UIButton alloc] initWithFrame:CGRectMake(45, 11, 8, 8)];
[_iconBtn addTarget:self action:@selector(selectCategoryAction) forControlEvents:UIControlEventTouchUpInside];
[_iconBtn setBackgroundImage:[UIImage imageNamed:@"btn_fliter_down"] forState:UIControlStateNormal];
}
return _iconBtn;
}
#pragma mark -- event
-(void)selectCategoryAction{
_curBlock();
}
-(void)setSearchCategory:(NSString*)title{
[_categoryBtn setTitle:title forState:UIControlStateNormal];
}
@end
@implementation MainSearchBar
- (UIButton *)scanBtn{
if (!_scanBtn) {
_scanBtn = [UIButton new];
[_scanBtn setImage:[UIImage imageNamed:@"button_scan"] forState:UIControlStateNormal];
[self addSubview:_scanBtn];
[_scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 30));
make.centerY.equalTo(self);
make.right.equalTo(self);
}];
}
return _scanBtn;
}
-(void)layoutSubviews
{
//fix width in ios7
self.width=kScreen_Width-115;
self.autoresizesSubviews = YES;
//找到输入框 右移
NSPredicate *finalPredicate = [NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary *bindings) {
return [candidateView isMemberOfClass:NSClassFromString(@"UISearchBarTextField")];
}];
UITextField *searchField = [[[[[self subviews] firstObject] subviews] filteredArrayUsingPredicate:finalPredicate] lastObject];
searchField.textAlignment = NSTextAlignmentLeft;
[searchField setFrame:CGRectMake(-CGRectGetWidth(self.frame)/2 + 40, 4.8, CGRectGetWidth(self.frame), 22)];
[(UIImageView*)searchField.leftView setSize:CGSizeMake(13, 13)];
}
@end