单军华
2018-07-19 83b9d5c682b21d88133f24da0f94dd56bd79e687
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
//  MOFSToolView.m
//  MOFSPickerManagerDemo
//
//  Created by 罗源 on 2018/2/5.
//  Copyright © 2018年 luoyuan. All rights reserved.
//
 
#import "MOFSToolView.h"
 
#define BAR_COLOR [UIColor colorWithRed:0.090  green:0.463  blue:0.906 alpha:1]
#define LINE_COLOR [UIColor colorWithRed:0.804  green:0.804  blue:0.804 alpha:1]
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
 
@implementation MOFSToolView
 
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        //self.translatesAutoresizingMaskIntoConstraints = false;
        
        _cancelBar = [UILabel new];
        _cancelBar.font = [UIFont systemFontOfSize:14];
        _cancelBar.textColor = BAR_COLOR;
        _cancelBar.text = @"取消";
        _cancelBar.textAlignment = NSTextAlignmentLeft;
        _cancelBar.userInteractionEnabled = true;
        [_cancelBar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAction)]];
        [self addSubview:_cancelBar];
        [_cancelBar setTranslatesAutoresizingMaskIntoConstraints:false];
        [_cancelBar setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        [_cancelBar setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        
        _titleBar = [UILabel new];
        _titleBar.font = [UIFont systemFontOfSize:14];
        _titleBar.text = @"标题";
        _titleBar.textAlignment = NSTextAlignmentCenter;
        _titleBar.textColor = LINE_COLOR;
        [self addSubview:_titleBar];
        [_titleBar setTranslatesAutoresizingMaskIntoConstraints:false];
        [_titleBar setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        
        _commitBar = [UILabel new];
        _commitBar.font = [UIFont systemFontOfSize:14];
        _commitBar.textColor = BAR_COLOR;
        _commitBar.text = @"完成";
        _commitBar.textAlignment = NSTextAlignmentRight;
        _commitBar.userInteractionEnabled = true;
        [_commitBar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commitAction)]];
        [self addSubview:_commitBar];
        [_commitBar setTranslatesAutoresizingMaskIntoConstraints:false];
        [_commitBar setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        [_commitBar setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        
        UIView *topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 0.5)];
        topLineView.backgroundColor = LINE_COLOR;
        [self addSubview:topLineView];
        [topLineView setTranslatesAutoresizingMaskIntoConstraints:false];
        
        UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, UISCREEN_WIDTH, 0.5)];
        bottomLineView.backgroundColor = LINE_COLOR;
        [self addSubview:bottomLineView];
        [bottomLineView setTranslatesAutoresizingMaskIntoConstraints:false];
        
        NSLayoutConstraint *constrant_a = [NSLayoutConstraint constraintWithItem:_cancelBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10];
        NSLayoutConstraint *constrant_b = [NSLayoutConstraint constraintWithItem:_cancelBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_c = [NSLayoutConstraint constraintWithItem:_cancelBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self addConstraints:@[constrant_a, constrant_b, constrant_c]];
        
        NSLayoutConstraint *constrant_d = [NSLayoutConstraint constraintWithItem:_titleBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_cancelBar attribute:NSLayoutAttributeRight multiplier:1.0 constant:10];
        NSLayoutConstraint *constrant_e = [NSLayoutConstraint constraintWithItem:_titleBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_f = [NSLayoutConstraint constraintWithItem:_titleBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self addConstraints:@[constrant_d, constrant_e, constrant_f]];
        
        NSLayoutConstraint *constrant_g = [NSLayoutConstraint constraintWithItem:_commitBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_titleBar attribute:NSLayoutAttributeRight multiplier:1.0 constant:10];
        NSLayoutConstraint *constrant_h = [NSLayoutConstraint constraintWithItem:_commitBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_i = [NSLayoutConstraint constraintWithItem:_commitBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_j = [NSLayoutConstraint constraintWithItem:_commitBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10];
        [self addConstraints:@[constrant_g, constrant_h, constrant_i, constrant_j]];
        
        NSLayoutConstraint *constrant_k = [NSLayoutConstraint constraintWithItem:topLineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_l = [NSLayoutConstraint constraintWithItem:topLineView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_m = [NSLayoutConstraint constraintWithItem:topLineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_n = [NSLayoutConstraint constraintWithItem:topLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.5];
        [self addConstraints:@[constrant_k, constrant_l, constrant_m, constrant_n]];
        
        NSLayoutConstraint *constrant_o = [NSLayoutConstraint constraintWithItem:bottomLineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_p = [NSLayoutConstraint constraintWithItem:bottomLineView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_q = [NSLayoutConstraint constraintWithItem:bottomLineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        NSLayoutConstraint *constrant_r = [NSLayoutConstraint constraintWithItem:bottomLineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.5];
        [self addConstraints:@[constrant_o, constrant_p, constrant_q, constrant_r]];
        
 
    }
    return self;
}
 
#pragma mark - Action
 
- (void)cancelAction {
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}
 
- (void)commitAction {
    if (self.commitBlock) {
        self.commitBlock();
    }
}
 
#pragma mark - install
 
- (void)setCancelBarTitle:(NSString *)cancelBarTitle {
    _cancelBarTitle = cancelBarTitle;
    if (self.cancelBar) {
        self.cancelBar.text = cancelBarTitle;
    }
}
 
- (void)setCancelBarTintColor:(UIColor *)cancelBarTintColor {
    _cancelBarTintColor = cancelBarTintColor;
    if (self.cancelBar) {
        self.cancelBar.tintColor = cancelBarTintColor;
    }
}
 
- (void)setCommitBarTitle:(NSString *)commitBarTitle {
    _commitBarTitle = commitBarTitle;
    if (self.commitBar) {
        self.commitBar.text = commitBarTitle;
    }
}
 
- (void)setCommitBarTintColor:(UIColor *)commitBarTintColor {
    _commitBarTintColor = commitBarTintColor;
    if (self.commitBar) {
        self.commitBar.tintColor = commitBarTintColor;
    }
}
 
- (void)setTitleBarTitle:(NSString *)titleBarTitle {
    _titleBarTitle = titleBarTitle;
    if (self.titleBar) {
        self.titleBar.text = titleBarTitle;
    }
}
 
- (void)setTitleBarTextColor:(UIColor *)titleBarTextColor {
    _titleBarTextColor = titleBarTextColor;
    if (self.titleBar) {
        self.titleBar.textColor = titleBarTextColor;;
    }
}
 
@end