From b838a7ae7017079c9643e1a140f11a291b263a74 Mon Sep 17 00:00:00 2001 From: azureatom Date: Wed, 4 Nov 2015 15:51:46 +0800 Subject: [PATCH 1/4] fix 2 bugs: 1. alertWindow still show after alertView dismissed. To duplicate this bug, just set alertView.backgroundColor = [UIColor redColor]; before show it. 2. tap Screen after alertView dismissed, iOS log error message: unexpected nil window in _UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow: (null) Solution: 1. remove invokes of removeFromSuperview for UIWindow instance. 2. make __si_alert_background_window and alertWindow hidden when dismiss alertView. --- SIAlertView/SIAlertView.m | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 14a392f..2695513 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -301,13 +301,11 @@ + (void)showBackground + (void)hideBackgroundAnimated:(BOOL)animated { void (^completion)(void) = ^{ - [__si_alert_background_window removeFromSuperview]; - __si_alert_background_window = nil; - UIWindow *mainWindow = [UIApplication sharedApplication].windows[0]; mainWindow.tintAdjustmentMode = UIViewTintAdjustmentModeNormal; [mainWindow makeKeyWindow]; - mainWindow.hidden = NO; + __si_alert_background_window.hidden = YES; + __si_alert_background_window = nil; }; if (!animated) { @@ -548,8 +546,6 @@ - (void)dismissAnimated:(BOOL)animated cleanup:(BOOL)cleanup } void (^dismissComplete)(void) = ^{ - self.visible = NO; - [self teardown]; [SIAlertView setCurrentAlertView:nil]; @@ -918,7 +914,7 @@ - (void)teardown self.titleLabel = nil; self.messageLabel = nil; [self.buttons removeAllObjects]; - [self.alertWindow removeFromSuperview]; + self.alertWindow.hidden = YES; self.alertWindow = nil; self.layoutDirty = NO; } From 6a74e16dc8637c9493e7a0dc37d98253ce872734 Mon Sep 17 00:00:00 2001 From: azureatom Date: Thu, 12 Nov 2015 16:58:50 +0800 Subject: [PATCH 2/4] Add UITextField to SIAlertView and handle UIReturnKeyDone. Adjust frame when keyboard appears. --- SIAlertView/SIAlertView.h | 4 +++- SIAlertView/SIAlertView.m | 48 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/SIAlertView/SIAlertView.h b/SIAlertView/SIAlertView.h index d65b7e9..7549d1b 100644 --- a/SIAlertView/SIAlertView.h +++ b/SIAlertView/SIAlertView.h @@ -40,10 +40,12 @@ typedef NS_ENUM(NSInteger, SIAlertViewTransitionStyle) { @class SIAlertView; typedef void(^SIAlertViewHandler)(SIAlertView *alertView); -@interface SIAlertView : UIView +@interface SIAlertView : UIView @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *message; +@property (nonatomic, strong) UITextField *textField; +@property (nonatomic, copy) void(^textFieldReturnBlock)(); @property (nonatomic, copy) NSAttributedString *attributedTitle; @property (nonatomic, copy) NSAttributedString *attributedMessage; diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 2695513..8663e2a 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -178,8 +178,9 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie #pragma mark - SIAlertView @implementation SIAlertView - @synthesize title = _title, message = _message; +@synthesize textField; +@synthesize textFieldReturnBlock; @synthesize attributedTitle = _attributedTitle, attributedMessage = _attributedMessage; + (void)initialize @@ -821,6 +822,12 @@ - (void)validateLayout self.messageLabel.frame = CGRectMake(CONTENT_PADDING_LEFT, y, CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, height); y += height + GAP; } + + if (self.textField) { + y += GAP; + self.textField.frame = CGRectMake(CONTENT_PADDING_LEFT, y, CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, 30); + y += 30 + GAP; + } contentContainerViewHeight = y; if (self.items.count > 0) { @@ -903,6 +910,7 @@ - (void)setup [self setupViewHierarchy]; [self updateTitleLabel]; [self updateMessageLabel]; + [self updateTextField]; [self setupButtons]; [self setupLineLayer]; } @@ -913,6 +921,11 @@ - (void)teardown self.containerView = nil; self.titleLabel = nil; self.messageLabel = nil; + if (self.textField) { + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; + self.textFieldReturnBlock = nil; + self.textField = nil; + } [self.buttons removeAllObjects]; self.alertWindow.hidden = YES; self.alertWindow = nil; @@ -992,6 +1005,39 @@ - (void)updateMessageLabel [self invalidateLayout]; } +-(void)updateTextField{ + if (self.textField) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; + self.textField.delegate = self; + [self.contentContainerView addSubview:self.textField]; + } +} + +// Called when the UIKeyboardDidShowNotification is sent. +- (void)keyboardWasShown:(NSNotification*)aNotification +{ + NSDictionary* info = [aNotification userInfo]; + CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; + + CGRect containerViewFrame = self.containerView.frame; + containerViewFrame.origin.y = self.frame.size.height - kbSize.height - containerViewFrame.size.height - 6; + if (containerViewFrame.origin.y > self.containerView.frame.origin.y) { + self.containerView.frame = containerViewFrame; + } +} + +#pragma mark - UITextFieldDelegate +- (BOOL)textFieldShouldReturn:(UITextField *)aTextField{ + if (aTextField == self.textField) { + [self.textField resignFirstResponder]; + if (textFieldReturnBlock) { + textFieldReturnBlock(); + } + return YES; + } + return NO; +} + - (void)setupButtons { self.buttons = [[NSMutableArray alloc] initWithCapacity:self.items.count]; From 73623c660de2a9b5107c2789ae91bddcb26377b1 Mon Sep 17 00:00:00 2001 From: azureatom Date: Fri, 25 Dec 2015 16:34:25 +0800 Subject: [PATCH 3/4] Adding auto rotation configuration. fixing bug: modify containerView position after keyboard popup. --- SIAlertView/SIAlertView.h | 3 +-- SIAlertView/SIAlertView.m | 11 ++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/SIAlertView/SIAlertView.h b/SIAlertView/SIAlertView.h index 7549d1b..48731ed 100644 --- a/SIAlertView/SIAlertView.h +++ b/SIAlertView/SIAlertView.h @@ -60,7 +60,7 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, copy) SIAlertViewHandler didDismissHandler; @property (nonatomic, readonly, getter = isVisible) BOOL visible; - +@property (nonatomic, assign) BOOL enableAutoRotation;//default NO, doesn't support autorotation or landscape. @property (nonatomic, assign) BOOL parallaxEffectEnabled; // theme @@ -81,7 +81,6 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, strong) UIColor *cancelButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *destructiveButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; - - (id)initWithTitle:(NSString *)title message:(NSString *)message; - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButton:(NSString *)canelButton handler:(SIAlertViewHandler)handler; - (id)initWithAttributedTitle:(NSAttributedString *)attributedTitle attributedMessage:(NSAttributedString *)attributedMessage; diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 8663e2a..07ce597 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -166,6 +166,14 @@ - (void)viewDidLoad [self.alertView setup]; } +-(BOOL)shouldAutorotate{ + return self.alertView.enableAutoRotation; +} + +-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ + return self.alertView.enableAutoRotation ? [super supportedInterfaceOrientations] : UIInterfaceOrientationMaskPortrait; +} + - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; @@ -182,6 +190,7 @@ @implementation SIAlertView @synthesize textField; @synthesize textFieldReturnBlock; @synthesize attributedTitle = _attributedTitle, attributedMessage = _attributedMessage; +@synthesize enableAutoRotation; + (void)initialize { @@ -1021,7 +1030,7 @@ - (void)keyboardWasShown:(NSNotification*)aNotification CGRect containerViewFrame = self.containerView.frame; containerViewFrame.origin.y = self.frame.size.height - kbSize.height - containerViewFrame.size.height - 6; - if (containerViewFrame.origin.y > self.containerView.frame.origin.y) { + if (containerViewFrame.origin.y < self.containerView.frame.origin.y) { self.containerView.frame = containerViewFrame; } } From b168dbd0d83dac1e9f2d51f0eef04e9a0288a7a5 Mon Sep 17 00:00:00 2001 From: azureatom Date: Thu, 25 Feb 2016 16:10:43 +0800 Subject: [PATCH 4/4] check __si_alert_background_window before show it. Hide and invalidate if it's already there. --- SIAlertView/SIAlertView.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 07ce597..69f335e 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -293,6 +293,9 @@ + (void)setAnimating:(BOOL)animating + (void)showBackground { + if (__si_alert_background_window != nil){ + [SIAlertView hideBackgroundAnimated:NO]; + } if (!__si_alert_background_window) { __si_alert_background_window = [[SIAlertBackgroundWindow alloc] initWithFrame:[UIScreen mainScreen].bounds andStyle:[SIAlertView currentAlertView].backgroundStyle];