Skip to content

Commit c92076a

Browse files
12207480tanyang
authored andcommitted
add textfield
1 parent 4b56da8 commit c92076a

File tree

6 files changed

+96
-22
lines changed

6 files changed

+96
-22
lines changed

TYAlertControllerDemo/Base.lproj/Main.storyboard

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="15A284" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
56
</dependencies>
67
<scenes>
78
<!--View Controller-->
@@ -18,6 +19,7 @@
1819
<subviews>
1920
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vCS-SU-E0R">
2021
<rect key="frame" x="257" y="103" width="87" height="30"/>
22+
<animations/>
2123
<constraints>
2224
<constraint firstAttribute="width" constant="87" id="EDr-Az-Wg8"/>
2325
<constraint firstAttribute="height" constant="30" id="M2D-4m-vPv"/>
@@ -31,6 +33,7 @@
3133
</button>
3234
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Pbc-5y-GLg">
3335
<rect key="frame" x="255" y="168" width="91" height="30"/>
36+
<animations/>
3437
<constraints>
3538
<constraint firstAttribute="width" constant="91" id="gIB-6P-PPI"/>
3639
<constraint firstAttribute="height" constant="30" id="zbZ-eV-dCO"/>
@@ -43,6 +46,7 @@
4346
</connections>
4447
</button>
4548
</subviews>
49+
<animations/>
4650
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
4751
<constraints>
4852
<constraint firstItem="Pbc-5y-GLg" firstAttribute="top" secondItem="vCS-SU-E0R" secondAttribute="bottom" constant="35" id="XMM-Dk-17g"/>

TYAlertControllerDemo/TYAlertController/TYAlertView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ typedef enum : NSUInteger {
3131
@property (nonatomic, weak, readonly) UILabel *titleLable;
3232
@property (nonatomic, weak, readonly) UILabel *messageLabel;
3333

34+
@property (nonatomic, strong) UIColor *textFieldBorderColor;
35+
3436
+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message;
3537

3638
- (void)addAction:(TYAlertAction *)action;
3739

40+
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
41+
3842
@end

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ - (id)copyWithZone:(NSZone *)zone
4343

4444
@end
4545

46+
4647
@interface TYAlertView ()
4748

4849
// text content View
@@ -51,6 +52,8 @@ @interface TYAlertView ()
5152
@property (nonatomic, weak) UILabel *messageLabel;
5253

5354
@property (nonatomic, weak) UIView *textFeildContentView;
55+
@property (nonatomic, weak) NSLayoutConstraint *textFeildTopConstraint;
56+
@property (nonatomic, strong) NSMutableArray *textFeilds;
5457

5558
// button content View
5659
@property (nonatomic, weak) UIView *buttonContentView;
@@ -63,9 +66,13 @@ @interface TYAlertView ()
6366

6467
@end
6568

66-
@implementation TYAlertView
67-
6869
#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
6976

7077
#pragma mark - init
7178

@@ -108,8 +115,11 @@ - (void)configureProperty
108115
_contentViewSpace = 15;
109116
_contentViewEdge = 12;
110117
_textLabelSpace = 6;
118+
111119
_buttons = [NSMutableArray array];
112120
_actions = [NSMutableArray array];
121+
122+
_textFieldBorderColor = kTextFeildBorderColor;
113123
}
114124

115125
#pragma mark - add contentview
@@ -165,6 +175,27 @@ - (void)addAction:(TYAlertAction *)action
165175
[self layoutButtons];
166176
}
167177

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+
168199
#pragma mark - layout contenview
169200

170201
- (void)layoutContentViews
@@ -174,18 +205,16 @@ - (void)layoutContentViews
174205

175206
[self addConstarintWithView:_textContentView topView:self leftView:self bottomView:nil rightView:self edageInset:UIEdgeInsetsMake(_contentViewEdge, _contentViewEdge, 0, -_contentViewEdge)];
176207

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
186215
_buttonContentView.translatesAutoresizingMaskIntoConstraints = NO;
187216

188-
[self addConstarintWithTopView:_textContentView toBottomView:_buttonContentView constarint:_contentViewSpace];
217+
[self addConstarintWithTopView:_textFeildContentView toBottomView:_buttonContentView constarint:_contentViewSpace];
189218

190219
[self addConstarintWithView:_buttonContentView topView:nil leftView:self bottomView:self rightView:self edageInset:UIEdgeInsetsMake(0, _contentViewEdge, -_contentViewEdge, -_contentViewEdge)];
191220
}
@@ -205,7 +234,7 @@ - (void)layoutButtons
205234
UIButton *button = _buttons.lastObject;
206235
if (_buttons.count == 1) {
207236
[button addConstraintToView:_buttonContentView edageInset:UIEdgeInsetsZero];
208-
[button addConstarintWidth:0 height:44];
237+
[button addConstarintWidth:0 height:KButtonHeight];
209238
}else if (_buttons.count == 2) {
210239
UIButton *firstButton = _buttons.firstObject;
211240
[_buttonContentView removeConstraintWithView:firstButton attribte:NSLayoutAttributeRight];
@@ -226,8 +255,31 @@ - (void)layoutButtons
226255
UIButton *lastSecondBtn = _buttons[_buttons.count-2];
227256
[_buttonContentView removeConstraintWithView:lastSecondBtn attribte:NSLayoutAttributeBottom];
228257
[_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];
231283
}
232284
}
233285

TYAlertControllerDemo/TYAlertController/UIView+TYAutoLayout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
- (void)addConstarintWithLeftView:(UIView *)leftView toRightView:(UIView *)rightView constarint:(CGFloat)constarint;
1919

20-
- (void)addConstarintWithTopView:(UIView *)topView toBottomView:(UIView *)bottomView constarint:(CGFloat)constarint;
20+
- (NSLayoutConstraint *)addConstarintWithTopView:(UIView *)topView toBottomView:(UIView *)bottomView constarint:(CGFloat)constarint;
2121

2222
- (void)addConstarintWidth:(CGFloat)width height:(CGFloat)height;
2323

TYAlertControllerDemo/TYAlertController/UIView+TYAutoLayout.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ - (void)addConstarintWithLeftView:(UIView *)leftView toRightView:(UIView *)right
4040
[self addConstraint:[NSLayoutConstraint constraintWithItem:leftView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:rightView attribute:NSLayoutAttributeLeft multiplier:1 constant:-constarint]];
4141
}
4242

43-
- (void)addConstarintWithTopView:(UIView *)topView toBottomView:(UIView *)bottomView constarint:(CGFloat)constarint
43+
- (NSLayoutConstraint *)addConstarintWithTopView:(UIView *)topView toBottomView:(UIView *)bottomView constarint:(CGFloat)constarint
4444
{
45-
[self addConstraint:[NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bottomView attribute:NSLayoutAttributeTop multiplier:1 constant:-constarint]];
45+
NSLayoutConstraint *topButtomConstraint =[NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:bottomView attribute:NSLayoutAttributeTop multiplier:1 constant:-constarint];
46+
[self addConstraint:topButtomConstraint];
47+
return topButtomConstraint;
4648
}
4749

4850
- (void)addConstarintWidth:(CGFloat)width height:(CGFloat)height

TYAlertControllerDemo/ViewController.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,24 @@ - (IBAction)showAlertView:(id)sender {
3636
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
3737

3838
}]];
39+
3940
[alertView addAction:[TYAlertAction actionWithTitle:@"默认1" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
4041

4142
}]];
42-
[alertView addAction:[TYAlertAction actionWithTitle:@"默认2" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
43+
// [alertView addAction:[TYAlertAction actionWithTitle:@"默认2" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
44+
//
45+
// }]];
46+
47+
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
4348

44-
}]];
49+
}];
50+
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
51+
52+
}];
53+
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
54+
55+
}];
56+
4557
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
4658
[self presentViewController:alertController animated:YES completion:nil];
4759
}

0 commit comments

Comments
 (0)