@@ -43,6 +43,7 @@ - (id)copyWithZone:(NSZone *)zone
43
43
44
44
@end
45
45
46
+
46
47
@interface TYAlertView ()
47
48
48
49
// text content View
@@ -51,6 +52,8 @@ @interface TYAlertView ()
51
52
@property (nonatomic , weak ) UILabel *messageLabel;
52
53
53
54
@property (nonatomic , weak ) UIView *textFeildContentView;
55
+ @property (nonatomic , weak ) NSLayoutConstraint *textFeildTopConstraint;
56
+ @property (nonatomic , strong ) NSMutableArray *textFeilds;
54
57
55
58
// button content View
56
59
@property (nonatomic , weak ) UIView *buttonContentView;
@@ -63,9 +66,13 @@ @interface TYAlertView ()
63
66
64
67
@end
65
68
66
- @implementation TYAlertView
67
-
68
69
#define kButtonTagOffset 1000
70
+ #define KButtonHeight 44
71
+ #define kTextFeildOffset 10000
72
+ #define kTextFeildHeight 30
73
+ #define kTextFeildBorderColor [UIColor colorWithRed: 203 /255.0 green: 203 /255.0 blue: 203 /255.0 alpha: 1 ];
74
+
75
+ @implementation TYAlertView
69
76
70
77
#pragma mark - init
71
78
@@ -108,8 +115,11 @@ - (void)configureProperty
108
115
_contentViewSpace = 15 ;
109
116
_contentViewEdge = 12 ;
110
117
_textLabelSpace = 6 ;
118
+
111
119
_buttons = [NSMutableArray array ];
112
120
_actions = [NSMutableArray array ];
121
+
122
+ _textFieldBorderColor = kTextFeildBorderColor ;
113
123
}
114
124
115
125
#pragma mark - add contentview
@@ -165,6 +175,27 @@ - (void)addAction:(TYAlertAction *)action
165
175
[self layoutButtons ];
166
176
}
167
177
178
+ - (void )addTextFieldWithConfigurationHandler : (void (^)(UITextField *textFeild))configurationHandler
179
+ {
180
+ if (_textFeilds == nil ) {
181
+ _textFeilds = [NSMutableArray array ];
182
+ }
183
+
184
+ UITextField *textField = [[UITextField alloc ]init];
185
+ textField.tag = kTextFeildOffset + _textFeilds.count ;
186
+ textField.backgroundColor = [UIColor whiteColor ];
187
+ textField.translatesAutoresizingMaskIntoConstraints = NO ;
188
+
189
+ if (configurationHandler) {
190
+ configurationHandler (textField);
191
+ }
192
+
193
+ [_textFeildContentView addSubview: textField];
194
+ [_textFeilds addObject: textField];
195
+
196
+ [self layoutTextFeilds ];
197
+ }
198
+
168
199
#pragma mark - layout contenview
169
200
170
201
- (void )layoutContentViews
@@ -174,18 +205,16 @@ - (void)layoutContentViews
174
205
175
206
[self addConstarintWithView: _textContentView topView: self leftView: self bottomView: nil rightView: self edageInset: UIEdgeInsetsMake (_contentViewEdge, _contentViewEdge, 0 , -_contentViewEdge)];
176
207
177
- // // textFeildContentView
178
- // _textFeildContentView.translatesAutoresizingMaskIntoConstraints = NO;
179
- // [self addConstraint:[NSLayoutConstraint constraintWithItem:_textFeildContentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
180
- //
181
- // [self addConstraint:[NSLayoutConstraint constraintWithItem:_textFeildContentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
182
- //
183
- // [self addConstraint:[NSLayoutConstraint constraintWithItem:_textFeildContentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
184
- //
185
- // buttonContentView
208
+ // textFeildContentView
209
+ _textFeildContentView.translatesAutoresizingMaskIntoConstraints = NO ;
210
+ _textFeildTopConstraint = [self addConstarintWithTopView: _textContentView toBottomView: _textFeildContentView constarint: 0 ];
211
+
212
+ [self addConstarintWithView: _textFeildContentView topView: nil leftView: self bottomView: nil rightView: self edageInset: UIEdgeInsetsMake (0 , _contentViewEdge, 0 , -_contentViewEdge)];
213
+
214
+ // buttonContentView
186
215
_buttonContentView.translatesAutoresizingMaskIntoConstraints = NO ;
187
216
188
- [self addConstarintWithTopView: _textContentView toBottomView: _buttonContentView constarint: _contentViewSpace];
217
+ [self addConstarintWithTopView: _textFeildContentView toBottomView: _buttonContentView constarint: _contentViewSpace];
189
218
190
219
[self addConstarintWithView: _buttonContentView topView: nil leftView: self bottomView: self rightView: self edageInset: UIEdgeInsetsMake (0 , _contentViewEdge, -_contentViewEdge, -_contentViewEdge)];
191
220
}
@@ -205,7 +234,7 @@ - (void)layoutButtons
205
234
UIButton *button = _buttons.lastObject ;
206
235
if (_buttons.count == 1 ) {
207
236
[button addConstraintToView: _buttonContentView edageInset: UIEdgeInsetsZero];
208
- [button addConstarintWidth: 0 height: 44 ];
237
+ [button addConstarintWidth: 0 height: KButtonHeight ];
209
238
}else if (_buttons.count == 2 ) {
210
239
UIButton *firstButton = _buttons.firstObject ;
211
240
[_buttonContentView removeConstraintWithView: firstButton attribte: NSLayoutAttributeRight];
@@ -226,8 +255,31 @@ - (void)layoutButtons
226
255
UIButton *lastSecondBtn = _buttons[_buttons.count-2 ];
227
256
[_buttonContentView removeConstraintWithView: lastSecondBtn attribte: NSLayoutAttributeBottom];
228
257
[_buttonContentView addConstarintWithTopView: lastSecondBtn toBottomView: button constarint: _textLabelSpace];
229
- [_buttonContentView addConstarintWithView: button topView: nil leftView: _buttonContentView bottomView: _buttonContentView rightView: nil edageInset: UIEdgeInsetsZero];
230
- [_buttonContentView addConstarintEqualWithView: button widthToView: lastSecondBtn heightToView: lastSecondBtn];
258
+ [_buttonContentView addConstarintWithView: button topView: nil leftView: _buttonContentView bottomView: _buttonContentView rightView: _buttonContentView edageInset: UIEdgeInsetsZero];
259
+ [_buttonContentView addConstarintEqualWithView: button widthToView: nil heightToView: lastSecondBtn];
260
+ }
261
+ }
262
+
263
+ - (void )layoutTextFeilds
264
+ {
265
+ UITextField *textFeild = _textFeilds.lastObject ;
266
+
267
+ if (_textFeilds.count == 1 ) {
268
+ // setup textFeildContentView
269
+ _textFeildContentView.backgroundColor = _textFieldBorderColor;
270
+ _textFeildContentView.layer .masksToBounds = YES ;
271
+ _textFeildContentView.layer .cornerRadius = 4 ;
272
+ _textFeildContentView.layer .borderWidth = 0.5 ;
273
+ _textFeildContentView.layer .borderColor = _textFieldBorderColor.CGColor ;
274
+ _textFeildTopConstraint.constant = -_contentViewSpace;
275
+ [textFeild addConstraintToView: _textFeildContentView edageInset: UIEdgeInsetsZero];
276
+ [textFeild addConstarintWidth: 0 height: kTextFeildHeight ];
277
+ }else {
278
+ UITextField *lastSecondTextFeild = _textFeilds[_textFeilds.count-2 ];
279
+ [_textFeildContentView removeConstraintWithView: lastSecondTextFeild attribte: NSLayoutAttributeBottom];
280
+ [_textFeildContentView addConstarintWithTopView: lastSecondTextFeild toBottomView: textFeild constarint: 0.5 ];
281
+ [_textFeildContentView addConstarintWithView: textFeild topView: nil leftView: _textFeildContentView bottomView: _textFeildContentView rightView: _textFeildContentView edageInset: UIEdgeInsetsZero];
282
+ [_textFeildContentView addConstarintEqualWithView: textFeild widthToView: nil heightToView: lastSecondTextFeild];
231
283
}
232
284
}
233
285
0 commit comments