Skip to content

Commit 443b2b7

Browse files
authored
Merge pull request 12207480#11 from feikaifeng/master
Fix some problems
2 parents e979e8f + a8e182d commit 443b2b7

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,25 +314,35 @@ - (void)singleTap:(UITapGestureRecognizer *)sender
314314
[self dismissViewControllerAnimated:YES];
315315
}
316316

317-
#pragma mark - notifycation
317+
#pragma mark - notification
318318

319-
- (void)keyboardWillShow:(NSNotification*)notification{
319+
- (void)keyboardWillShow:(NSNotification*)notification
320+
{
320321
CGRect keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
321322

322323
CGFloat alertViewBottomEdge = (CGRectGetHeight(self.view.frame) - CGRectGetHeight(_alertView.frame))/2 - _alertViewCenterYOffset;
324+
325+
//当开启热点时,向下偏移20px
326+
//修复键盘遮挡问题
327+
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
323328
CGFloat differ = CGRectGetHeight(keyboardRect) - alertViewBottomEdge;
329+
330+
//修复:输入框获取焦点时,会重复刷新,导致输入框文章偏移一下
331+
if (_alertViewCenterYConstraint.constant == -differ -statusBarHeight) {
332+
return;
333+
}
324334

325335
if (differ >= 0) {
326-
_alertViewCenterYConstraint.constant = _alertViewCenterYOffset - differ;
336+
_alertViewCenterYConstraint.constant = _alertViewCenterYOffset - differ - statusBarHeight;
327337
[UIView animateWithDuration:0.25 animations:^{
328338
[self.view layoutIfNeeded];
329339
}];
330340
}
331341
}
332342

333343

334-
- (void)keyboardWillHide:(NSNotification*)notification{
335-
344+
- (void)keyboardWillHide:(NSNotification*)notification
345+
{
336346
_alertViewCenterYConstraint.constant = _alertViewCenterYOffset;
337347
[UIView animateWithDuration:0.25 animations:^{
338348
[self.view layoutIfNeeded];
@@ -348,7 +358,6 @@ - (void)dealloc
348358
{
349359
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
350360
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
351-
//NSLog(@"%@ dealloc",NSStringFromClass([self class]));
352361
}
353362

354363
@end

TYAlertControllerDemo/TYAlertController/TYAlertView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef NS_ENUM(NSUInteger, TYAlertActionStyle) {
6060
@property (nonatomic, strong) UIFont *textFieldFont;
6161
@property (nonatomic, assign) CGFloat textFieldHeight;
6262
@property (nonatomic, assign) CGFloat textFieldEdge;
63-
@property (nonatomic, assign) CGFloat textFieldorderWidth;
63+
@property (nonatomic, assign) CGFloat textFieldBorderWidth;
6464
@property (nonatomic, assign) CGFloat textFieldContentViewEdge;
6565

6666

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ @interface TYAlertView ()
7676
#define kButtonSpace 6
7777
#define KButtonHeight 44
7878

79-
#define ktextFieldOffset 10000
80-
#define ktextFieldHeight 29
81-
#define ktextFieldEdge 8
82-
#define KtextFieldBorderWidth 0.5
79+
#define kTextFieldOffset 10000
80+
#define kTextFieldHeight 29
81+
#define kTextFieldEdge 8
82+
#define KTextFieldBorderWidth 0.5
8383

8484

8585
@implementation TYAlertView
@@ -132,14 +132,14 @@ - (void)configureProperty
132132
_buttonContentViewEdge = kContentViewEdge;
133133
_buttonContentViewTop = kContentViewSpace;
134134
_buttonCornerRadius = 4.0;
135-
_buttonFont = [UIFont fontWithName:@"HelveticaNeue" size:18];;
135+
_buttonFont = [UIFont fontWithName:@"HelveticaNeue" size:18];
136136
_buttonDefaultBgColor = [UIColor colorWithRed:52/255.0 green:152/255.0 blue:219/255.0 alpha:1];
137137
_buttonCancelBgColor = [UIColor colorWithRed:127/255.0 green:140/255.0 blue:141/255.0 alpha:1];
138138
_buttonDestructiveBgColor = [UIColor colorWithRed:231/255.0 green:76/255.0 blue:60/255.0 alpha:1];
139139

140-
_textFieldHeight = ktextFieldHeight;
141-
_textFieldEdge = ktextFieldEdge;
142-
_textFieldorderWidth = KtextFieldBorderWidth;
140+
_textFieldHeight = kTextFieldHeight;
141+
_textFieldEdge = kTextFieldEdge;
142+
_textFieldBorderWidth = KTextFieldBorderWidth;
143143
_textFieldContentViewEdge = kContentViewEdge;
144144

145145
_textFieldBorderColor = [UIColor colorWithRed:203/255.0 green:203/255.0 blue:203/255.0 alpha:1];
@@ -241,7 +241,7 @@ - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))c
241241
}
242242

243243
UITextField *textField = [[UITextField alloc]init];
244-
textField.tag = ktextFieldOffset + _textFields.count;
244+
textField.tag = kTextFieldOffset + _textFields.count;
245245
textField.font = _textFieldFont;
246246
textField.translatesAutoresizingMaskIntoConstraints = NO;
247247

@@ -263,7 +263,7 @@ - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))c
263263
[_textFieldSeparateViews addObject:separateView];
264264
}
265265

266-
[self layouttextFields];
266+
[self layoutTextFields];
267267
}
268268

269269
- (NSArray *)textFieldArray
@@ -338,20 +338,20 @@ - (void)layoutButtons
338338
[_buttonContentView removeConstraintWithView:firstBtn attribute:NSLayoutAttributeRight];
339339
[_buttonContentView removeConstraintWithView:firstBtn attribute:NSLayoutAttributeBottom];
340340
[_buttonContentView removeConstraintWithView:secondBtn attribute:NSLayoutAttributeTop];
341-
[_buttonContentView addConstraintWithView:firstBtn topView:nil leftView:nil bottomView:0 rightView:_buttonContentView edgeInset:UIEdgeInsetsZero];
341+
[_buttonContentView addConstraintWithView:firstBtn topView:nil leftView:nil bottomView:nil rightView:_buttonContentView edgeInset:UIEdgeInsetsZero];
342342
[_buttonContentView addConstraintWithTopView:firstBtn toBottomView:secondBtn constant:_buttonSpace];
343343

344344
}
345345

346-
UIButton *lastSecondBtn = _buttons[_buttons.count-2];
346+
UIButton *lastSecondBtn = _buttons[_buttons.count - 2];
347347
[_buttonContentView removeConstraintWithView:lastSecondBtn attribute:NSLayoutAttributeBottom];
348348
[_buttonContentView addConstraintWithTopView:lastSecondBtn toBottomView:button constant:_buttonSpace];
349349
[_buttonContentView addConstraintWithView:button topView:nil leftView:_buttonContentView bottomView:_buttonContentView rightView:_buttonContentView edgeInset:UIEdgeInsetsZero];
350350
[_buttonContentView addConstraintEqualWithView:button widthToView:nil heightToView:lastSecondBtn];
351351
}
352352
}
353353

354-
- (void)layouttextFields
354+
- (void)layoutTextFields
355355
{
356356
UITextField *textField = _textFields.lastObject;
357357

@@ -360,24 +360,24 @@ - (void)layouttextFields
360360
_textFieldContentView.backgroundColor = _textFieldBackgroudColor;
361361
_textFieldContentView.layer.masksToBounds = YES;
362362
_textFieldContentView.layer.cornerRadius = 4;
363-
_textFieldContentView.layer.borderWidth = _textFieldorderWidth;
363+
_textFieldContentView.layer.borderWidth = _textFieldBorderWidth;
364364
_textFieldContentView.layer.borderColor = _textFieldBorderColor.CGColor;
365365
_textFieldTopConstraint.constant = -_contentViewSpace;
366-
[_textFieldContentView addConstraintToView:textField edgeInset:UIEdgeInsetsMake(_textFieldorderWidth, _textFieldEdge, -_textFieldorderWidth, -_textFieldEdge)];
366+
[_textFieldContentView addConstraintToView:textField edgeInset:UIEdgeInsetsMake(_textFieldBorderWidth, _textFieldEdge, -_textFieldBorderWidth, -_textFieldEdge)];
367367
[textField addConstraintWidth:0 height:_textFieldHeight];
368368
}else {
369369
// textField
370-
UITextField *lastSecondtextField = _textFields[_textFields.count - 2];
371-
[_textFieldContentView removeConstraintWithView:lastSecondtextField attribute:NSLayoutAttributeBottom];
372-
[_textFieldContentView addConstraintWithTopView:lastSecondtextField toBottomView:textField constant:_textFieldorderWidth];
373-
[_textFieldContentView addConstraintWithView:textField topView:nil leftView:_textFieldContentView bottomView:_textFieldContentView rightView:_textFieldContentView edgeInset:UIEdgeInsetsMake(0, _textFieldEdge, -_textFieldorderWidth, -_textFieldEdge)];
374-
[_textFieldContentView addConstraintEqualWithView:textField widthToView:nil heightToView:lastSecondtextField];
370+
UITextField *lastSecondTextField = _textFields[_textFields.count - 2];
371+
[_textFieldContentView removeConstraintWithView:lastSecondTextField attribute:NSLayoutAttributeBottom];
372+
[_textFieldContentView addConstraintWithTopView:lastSecondTextField toBottomView:textField constant:_textFieldBorderWidth];
373+
[_textFieldContentView addConstraintWithView:textField topView:nil leftView:_textFieldContentView bottomView:_textFieldContentView rightView:_textFieldContentView edgeInset:UIEdgeInsetsMake(0, _textFieldEdge, -_textFieldBorderWidth, -_textFieldEdge)];
374+
[_textFieldContentView addConstraintEqualWithView:textField widthToView:nil heightToView:lastSecondTextField];
375375

376376
// separateview
377377
UIView *separateView = _textFieldSeparateViews[_textFields.count - 2];
378378
[_textFieldContentView addConstraintWithView:separateView topView:nil leftView:_textFieldContentView bottomView:nil rightView:_textFieldContentView edgeInset:UIEdgeInsetsZero];
379379
[_textFieldContentView addConstraintWithTopView:separateView toBottomView:textField constant:0];
380-
[separateView addConstraintWidth:0 height:_textFieldorderWidth];
380+
[separateView addConstraintWidth:0 height:_textFieldBorderWidth];
381381
}
382382
}
383383

0 commit comments

Comments
 (0)